javascript - An object contains a collection of objects - each with an id and name, how do I separate them? -


at moment have object:

obj = [object{id: 1, name: test, parentid: 3}, object{id:1, name: another, parentid: 5}, object{id:2, name:something, parentid: 1}]. 

ultimately want object structured in different manner. object, if identifier 1, has collection of names (in case test, , another). if 2, shows 'something'.

i can't work out how should look, so? obj = [[1,test], [1,another], [2,something]] right?

so if call obj[1] multiples (test, etc).

can help? i've been fiddling hour now, don't understand.

i built original object this:

var obj = array(); //loop here   var obj = {      id: id,      name: name,      parentid: parentid   };   obj.push(obj); 

what did wrong? how can fix this? got me object within objects thing, want id's , names within id's , names. ultimate goal iterate through this. out names of of id, way can use populate array or count

thus:

if(obj[id] == 1){   //put name in new array } 

is ultimate goal. i've gotten bit lost initial object creation big mess.

try:

var obj = [{id: 1, name: "foo"}, {id: 2, name: "fee"}, {id: 3, name: "fii"}]; var result = [], item, = 0; while(item = obj[i++]){     for(key in item){         if(key === "id") continue; //remove line if want keep id in array         !result[i] && (result[i] = []);         result[i].push(item[key]);     } } console.log(result[1]); // --> ["foo"] 

Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -