underscore.js - How to add new elements to the object of an array on javascript -
underscore.js - How to add new elements to the object of an array on javascript -
i have var arr = [{name:"joe"}, {name:"mark"}];
have age array var agearr = [{age: 24}, {age: 30}]
i need programatically add together respective age of objects
my array need looks var arr = [{name:"joe", age: 24}, {name:"mark", age: 30}];
i using javascript , included library underscore.js.
is there cleaner way accomplish this. 1 help code snippet this.
you can
var newarr = arr.map(function(v, i) { homecoming _.extend(v, agearr[i]); });
if don't using free variable agearr
, access index - zip
them first:
var newarr = _.zip(arr, agearr).map(function(v) { homecoming _.extend(v[0], v[1]); });
jsfiddle: http://jsfiddle.net/wrq2lo8m/
javascript underscore.js
great
ReplyDelete