javascript - Find and update an object in an array -
i want object array of objects, update it.
var myobjs = [{ id: 1, name: "foo"}, { id: 2, name: "bar" }]; var myobjecttoupdate = _.findwhere(myobjs, { id: 2 }); myobjecttoupdate = { id: 2, name: "boop" }; myobjs[1] // { id: 2, name: "boop" }
currently when update myobject in 3rd line, not update array of objects. i'm assuming updating new variable instead of referencing.
what correct way this?
@e_net4 correct, reassigning object found.
if need update name, try this:
var myobjs = [{ id: 1, name: "foo"}, { id: 2, name: "bar" }]; var myobjecttoupdate = _.findwhere(myobjs, { id: 2 }); myobjecttoupdate.name = "boop"; myobjs[1] // { id: 2, name: "boop" }
Comments
Post a Comment