Ted
Ted

Reputation: 3875

how to create an array of objects based on another array of objects?

I have this array:

var arr1 = [
 {"user":"dan","liked":"yes","age":"22"},
 {"user":"sarah","liked":"no","age":"21"},
 {"user":"john","liked":"yes","age":"23"},
];

I'd like to create a new (sub)array of that array, containing only the likes of the users.

so it would look like this:

var arr2 = [
 {"dan":"yes"},
 {"sarah":"no"},
 {"john":"yes"},
];

I tried:

var arr2 =[];
for(var i in arr1){
  arr2.push({[i[user]]:i[liked]});
}

it needs a tweak, ideas?

Upvotes: 18

Views: 18294

Answers (8)

Jaison James
Jaison James

Reputation: 4552

array.map will help you, for you understanding since map returns a new array based on your logic, The following example is simple and readable

const arr1 = [
  {"user":"dan","liked":"yes","age":"22"},
  {"user":"sarah","liked":"no","age":"21"},
  {"user":"john","liked":"yes","age":"23"},
 ];
 
 const arr2 = arr1.map(({user, liked}) => ({ [user]:liked }));
 
 console.log(arr2);

Upvotes: 1

Wael Chorfan
Wael Chorfan

Reputation: 784

if it is an array of objects

var arr1=[{},{},..]

and you have to loop through the objects attributes so you need this

var arr2 =[]



 arr1.forEach(element => {
      var temp = new Object();
      for (var name in element) {
        temp[name] = element[name]
      }
      arr2 .push(temp)
    });

now you have arr2 as an other object different from arr1

Upvotes: 0

Joseph
Joseph

Reputation: 119837

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);

Upvotes: 38

Pranoy Sarkar
Pranoy Sarkar

Reputation: 2233

var arr2=[];
arr1.forEach(function(obj)
{
    var temp=new Object();
    temp[obj.user]=obj.liked;
    arr2.push(temp);
});

I think this is what you want

Upvotes: 1

ibrahim mahrir
ibrahim mahrir

Reputation: 31682

var arr1 = [
 {"user":"dan","liked":"yes","age":"22"},
 {"user":"sarah","liked":"no","age":"21"},
 {"user":"john","liked":"yes","age":"23"},
];

var res = arr1.map(function(o) {
  var r = {};
  r[o.user] = o.liked;
  return r;
});

console.log(res);

Or in recent ECMAScript versions:

var arr1 = [
 {"user":"dan","liked":"yes","age":"22"},
 {"user":"sarah","liked":"no","age":"21"},
 {"user":"john","liked":"yes","age":"23"},
];

var res = arr1.map(o => ( { [o.user]: o.liked } ));

console.log(res);

Upvotes: 3

BiJ
BiJ

Reputation: 1689

Try this

var arr2 =[];
for(var i in arr1){
    var obj= {};
    obj[arr1[i]['user']] = arr1[i]['liked'];
    arr2.push(obj)
} 

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386560

You could map the deconstructed wanted properties to a new object.

var array1 = [{ user: "dan", liked: "yes", age: "22" }, { user: "sarah", liked: "no", age: "21" }, { user: "john", liked: "yes", age: "23" }],
    array2 = array1.map(({ user, liked }) => ({ [user]: liked }));

console.log(array2);

ES5

var array1 = [{ user: "dan", liked: "yes", age: "22" }, { user: "sarah", liked: "no", age: "21" }, { user: "john", liked: "yes", age: "23" }],
    array2 = array1.map(function (o) {
        var temp = {};
        temp[o.user] = o.liked;
        return temp;
    });

console.log(array2);

Upvotes: 1

Chad
Chad

Reputation: 618

Based on your edit:

arr1 = arr1.map(function(item){
  return{
    [item.user]: item.liked
  }
});

Upvotes: 3

Related Questions