I'm trying to convert a JavaScript object to an other.
Let's say that my JavaScript input object looks like this :
$scope.name_person= [{
"firstName":"Jean","lastName":"Smith"
}]
The output should be like this :
$scope.name_perso= [{
"values":[
"firstName":"Jean","lastName":"Smith"
]
}]
this is my code :
function convert(arr) {
return $.map(unique(arr), function(name){
return {
values: $.grep(arr, function(item){
return item.name == name
})
}
});
}
function unique(arr) {
var result = [];
$.map(arr, function(item){
if ($.inArray(item.name, result))
result.push(item.name);
})
return result;
}
$scope.data=convert($scope.name_person);
Any Adivice ?