javascript - In Lodash.js/Underscore.js, how to add index for every element? -


here input:

[{animal: "cat"}, {animal:"dog}} 

and output :

[{animal: "cat", idx: 1}, {animal: "dog", idx: 2}] 

does have ideas how in lodash/underscore.js?

in underscore:

you use either .map or .each iterate across every element in target array looking add indexes to. use _.extend add "idx" prop.

working example:

var data =  [{animal: "cat"}, {animal:"dog"}]  _.map(data, function(e, i) {   return _.extend(e, {idx: + 1}); }); 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -