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

timeout - Handshake_timeout on RabbitMQ using python and pika from remote vm -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

c# - Search and Add Comment with OpenXML for Word -