I have the following array;
["Test1", "Test2", "Test3"]
I want it to be like this;
[{ id: 0, name: "Test1" }, { id: 1, name: "Test2" }, { id: 2, name: "Test3" }]
Any Help?
I guess I should somehow use _.object()
for this mission.
As you are using angular this should simply do the trick.
var newArray = [];
angular.forEach(myArray,function(item, key){
newArray.push({id:key, name:item});
});
The following solution does not require angular and is just based on lodash:
var newArray = [];
_.forEach(["Test1", "Test2", "Test3"], function (item, key) {
newArray.push({
id: key,
name: item
});
});
The map() function is your friend:
_.map(array, function(item, index) {
return { id: index, name: item };
});