Reputation: 1422
I have a list in AngularJS, $scope.list[];
What I want to know is how can I fill that list to fit a key,value structure like this:
$scope.list[{key,value},{key,value}];
and I want to fill that "map" with data coming in json format:
{
exportData {
id: 1,
name : "Peter",
lastname : "Smith"
age : 36
}
}
Where the Id is going to be the KEY and the rest of the strucure is going to be de VALUE
For example in a structure like this:
[
1: {
name : "Peter",
lastname : "Smith"
age : 36
},
2: {
name : "John",
lastname : "Carlos"
age : 40
},
]
Upvotes: 0
Views: 25440
Reputation: 2702
I written the below code as per your need, hope it will help you:
var data = [
{
id: 1,
name : "Peter",
lastname : "Smith",
age : 36
}, {
id: 2,
name : "Peter",
lastname : "Smith",
age : 36
}
];
$scope.itemList = [];
angular.forEach(data, function(item){
var obj = {};
var valObj = {};
valObj.name = item.name;
valObj.lastname = item.lastname;
valObj.age = item.age;
obj[item.id] = valObj;
$scope.itemList.push(obj);
});
Upvotes: 4
Reputation: 755
Hope this function will help you
$scope.transform = function(exportData){
var _value = {};
_value.name = exportData.name;
_value.lastname = exportData.lastname;
_value.age = exportData.age;
var item = [exportData.id, _value];
$scope.list.push(item);
}
Upvotes: 1