In Lodash.js / Underscore.js, how do I add an index for each item?
4 answers
In a substring:
You can use .map or .each to iterate over each element of the target array you wanted to add indices to. Then you can use _. Extend to add an idx prop.
Working example:
var data = [{animal: "cat"}, {animal:"dog"}]
_.map(data, function(e, i) {
return _.extend(e, {idx: i + 1});
});
+2
source to share