I need help converting a JavaScript object to another format.
For example, my input object in JavaScript looks like this:
$scope.name_person= [{
"firstName":"Jean","lastName":"Smith"
}]
The desired output should look like this:
$scope.name_perso= [{
"values":[
"firstName":"Jean","lastName":"Smith"
]
}]
This is the code I have so far:
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 advice on improving this conversion process?