nburk
nburk

Reputation: 22731

How to add field to each object in Javascript array without loop?

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

Answers (1)

lovethebomb
lovethebomb

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

Related Questions