Reputation: 22731
Is there a way to add a field to each object of a Javascript array without looping over it?
something like
array.each(function (index, object){
object[newField] = anotherArray[index];
});
Upvotes: 2
Views: 4085
Reputation: 293
You might be interested in the differences between an Object ({}) and an Array ([]).
Currently, a cached length loop offers the best performance for an Array.
But Array also offers a method forEach
:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
Upvotes: 2