I have been having issues with this and I am just not getting the right technique implemented. It is kinda specific so I have had trouble tracking down an explanation online so far. Ok here it is:
I am looping through a large json object and creating a second object that has in it objects containing the information which I want to use later.
items = {};
$(data).each(function () {
i = 0;
$(list).each(function () {
if (list[i]["name"] === category) {
add = false;
}
i++;
});
if (add === true) {
list.push({name: category,info: 0, items: items});
}
My issue is this when I then try to add an item to items in a particular category, it add it to all categories, so:
list[0]["items"]["msg"] = "Test";
Adds test to all my category objects, and not simply to the first one. Can anyone tell me why?
1 Answer 1
Adds test to all my category objects, and not simply to the first one. Can anyone tell me why?
It adds the msg property to the one, single items object which is references from all the objects in the list. If you want distinctive objects, use
list.push({name: category,info: 0, items: {}});
instead.
ineeded for?