Click here to Skip to main content
15,898,588 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

Group isn't the right word, but it does effect the result I need.

I will have an array of objects that include "Category".
I need to create a new array (list?) of objects that look like this:

{Category: "Details", Items: [{ etc...}] }.

How do I do this?

Thanks ^_^

PS: this is my base object constructor to give you an idea:
JavaScript
var Change = function (category, item, from, to, requiresAuth) {
    this.Category = category;
    this.Item = item;
    this.From = from;
    this.To = to;
    if (requiresAuth)
        this.Severity = "alert-danger";
    else
        this.Severity = "alert-warning";
}



EDIT: I did not explain my requirements well.

I have an array of Change() types.
I need to transform that into an array grouped by Category. More like this:

JavaScript
var Change = function (category) {
    this.Category = category;
    this.Items = []
}
var Item = function(item, from, to, requiresAuth){
    this.Item = item;
    this.From = from;
    this.To = to;
    if (requiresAuth)
        this.Severity = "alert-danger";
    else
        this.Severity = "alert-warning";
}


I used JSON syntax as shorthand for the above. Sorry for not making that clear
Posted
Updated 25-Aug-15 2:39am
v2

1 solution

First thing to understand: with JavaScript, you don't need lists or other collections. There is only one collection, a JavaScript object (of type 'object'), which has one specialized form, array, with only one difference: it has the property length. Everything is organized as key-value pairs, elements of an associative array, which gives your the time complexity of O(1) for search-by-key:
https://en.wikipedia.org/wiki/JavaScript#Dynamic[^],
https://en.wikipedia.org/wiki/Associative_array[^],
https://en.wikipedia.org/wiki/Big_O_notation[^],
https://en.wikipedia.org/wiki/Time_complexity[^].

This is the "regular" object:
JavaScript
var object = {a:1, b:"some value"};

The only difference for array is that is has integer-indexed properties which are automatically counted under the property length, for example:
JavaScript
var array = ["a", "b", "c"];
array.someProperty = 12323;
array[13] = 12;

Notably, you can always add a "regular" property (someProperty; in this example) to any object (array, function, anything). The length in this example is 14. All the indexed values under any integer indices except assigned are undefined.

The remaining piece is using new. If a function is called with new, it returns some object from prototype (even if you don't have a prototype, some default prototype object is used), and the clone of prototype is returned, with all properties you assign through .this added.

To make and example of what you want, I would need to know what do you want to have in items. As I have no idea, you can think about it yourself, say, add the items later. Also, I want to fix your big problem: hard-coded immediate constants like "alert-warning". So, you can have:
JavaScript
// analog of enum:
var Severity = {alertDanger:1, alertWarning:0}

var Change = function (category, items) {
    this.Category = category;
    if (!items)
       this.Items = []
}

var Item = function(item, from, to, requiresAuth) {
    this.Item = item;
    this.From = from;
    this.To = to;
    if (requiresAuth)
        this.Severity = Severity.alertDanger;
    else
        this.Severity = Severity.alertWarning;
}

var itemsArray = [new Item("item1", 1, 3), new Item("item 2", 3, 4, true)]
var firstChange = new Change("NoItems");
var secondChange = new Change("Details", itemsArray);


[EDIT] This code sample was modified after your clarifications of the question. Feel free to ask your follow-up questions if something is not clear.

For working with arrays (which you may not need), see the first code sample and my explanations.
For working with prototypes and prototype chains, please see my recent answer: What is happening there related with prototypes?[^].

See also:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects[^],
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object[^].

—SA
 
Share this answer
 
v6
Comments
Maciej Los 25-Aug-15 11:35am    
Maestro!
Sergey Alexandrovich Kryukov 25-Aug-15 13:04pm    
:-)...
Thank you, Maciej.
—SA
Andy Lanng 25-Aug-15 11:37am    
To be honest, I reworked the logic anyway.

I am still passing populate and save methods up to a .js. It's the best way I'b found to get several controls talking to each over via javascript, hence the arrays :S
Sergey Alexandrovich Kryukov 25-Aug-15 13:06pm    
Thank you, Andy.
I did not say you should not use arrays. As soon as you need to traverse the object by integer index, they are valuable.
—SA
Andy Lanng 26-Aug-15 3:51am    
I may post what I am doing in a future question to see if there is a better way. ATM this makes sense to me and my deadline is fast approaching :S
Thanks as always ^_^

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900