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

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -