1
storeChildren : function(coreid, data) {
  $.each(data, function(id, type) {
    $.core.children[coreid] = {};
    $.core.children[coreid]['id'] = id;
    $.core.children[coreid]['type'] = type;
  });
}

($.core.children is an object)

I want the resulting array/object (I prefer objects) to look like this (excuse my PHP pseudocode):

array(coreid => array(id=>type, id=>type, id=>type));

I know I am overwriting $.core.children[coreid] each time I run the loop I just don't know how to fix it.

1
  • I'd use an object instead var obj = { key: value, key: value, ... } Commented Apr 7, 2012 at 4:40

2 Answers 2

2

If all your 'id's are unique, the following should work; is this what you're after?

storeChildren : function(coreid, data) {
  $.core.children[coreid] = {};
  $.each(data, function(id, type) {
    $.core.children[coreid][id] = type;
  });
}
Sign up to request clarification or add additional context in comments.

Comments

0
init: function() {
    $.core.children = []; // instantiate array
},

storeChildren : function(coreid, data) {

    $.each(data, function(id, type) {
        $.core.children[coreid] = {id : type};
    });
}

Instantiate the object elsewhere in your code, then you won't kill the associative array/object each time. The above example also uses an object inside your array instead of an array, the result, in JSON notation, looks as follows:

[ {key,value} , {key,value} , {key,value} , ...]

The coreid is the index of the array.

Comments

Your Answer

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.