arrays - Javascript how to add new object to object in a loop -
i need create array of objects 1
var vegetables = "babana": {"store": store, "foo": foo}, "tomato": {"store": store, "foo": foo}, "orange": {"store": store, "foo": foo};
im getting data loop , loop one:
for(var v in vegetablesdata) { // code dosent work "=" adding last entry array // data of 'store' , 'foo' taken somewhere other place code, didnt wrote where, because im thinking irrelevant question test[vegetables[v]] = {"store": store, "foo": foo}; }
the output need should in format (using json.stringify(vegetables)):
{"vegetables": {"babana": {"store": store, "foo": foo}, {"tomato": {"store": store, "foo": foo}, "orange": {"store": store, "foo": foo}}
i tied
.push
but not working. laso tried += , still dosent work. i'm noob, need help. thanks!
in example, you're not working on array.
the for
syntax you're using if in fact iterating on keys of object. if want add object object, use key , value this.
obj[key] = value
in example, you're setting content of vegetable
key v
key of object `test.
for(var v in vegetablesdata) { test[v] = {"store": store, "foo": foo}; }
it's not clear have in test, might end doing this:
test['vegetables'] = {} for(var v in vegetablesdata) { test['vegetables'][v] = {"store": store, "foo": foo}; }
Comments
Post a Comment