0

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?

Bergi
671k162 gold badges1k silver badges1.5k bronze badges
asked Mar 22, 2013 at 12:51
2
  • Please post your whole script, not only this fragment. What is i needed for? Commented Mar 22, 2013 at 13:04
  • Would you mind posting part of the json struct? It is hard to follow because the content of the list var is not there. Commented Mar 22, 2013 at 13:10

1 Answer 1

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.

answered Mar 22, 2013 at 13:07
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.