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
Post a Comment