how to create an array of objects based on another array of objects?
Based on your edit:
arr1 = arr1.map(function(item){
return{
[item.user]: item.liked
}
});
Use array.map
var arr1 = [
{"user":"dan","liked":"yes","age":"22"},
{"user":"sarah","liked":"no","age":"21"},
{"user":"john","liked":"yes","age":"23"},
];
var arr2 = arr1.map(v => ({ user: v.user, liked: v.liked }));
console.log(arr2);
With your update, although it can be done with array.map
, I recommend you use a key-value pair structure instead. You'd need array.reduce
.
var arr1 = [
{"user":"dan","liked":"yes","age":"22"},
{"user":"sarah","liked":"no","age":"21"},
{"user":"john","liked":"yes","age":"23"},
];
var arr2 = arr1.reduce((c, v) => (c[v.user] = v.liked, c) , {});
console.log(arr2);