-1

I want to convert array of object to a desired format in javascript, Please help me to resolve this.

const obj = [
  {name: 'parry1', age:22, height:5},
  {name: 'parry 2', age:23, height:6},
  {name: 'parry3', age:24, height:7}
]

// output = {name:['parry1', 'parry 2', 'parry3'], age:[22, 23, 24], height:[5, 6, 7]}

6 Answers6

1

Hope this helps you !

const obj = [
  {name: 'parry1', age:22, height:5},
  {name: 'parry 2', age:23, height:6},
  {name: 'parry3', age:24, height:7}
];

function ArrayToObject(array){
  const newObj={
    name: [],
    age: [],
    height: []
  };
  array.forEach(function(row){
    newObj.name.push(row["name"]);
    newObj.age.push(row["age"]);
    newObj.height.push(row["height"]);
  });
  return newObj;
}

console.log( ArrayToObject(obj) );
output = {name:['parry1', 'parry 2', 'parry3'], age:[22, 23, 24], height:[5, 6, 7]}
Adnane Ar
  • 683
  • 7
  • 11
1

This is a good use case for the zip function:

let zip = (...a) => a[0].map((_, i) => a.map(b => b[i]))

const obj = [
    {name: 'parry1', age: 22, height: 5},
    {name: 'parry 2', age: 23, height: 6},
    {name: 'parry3', age: 24, height: 7}
]

res = Object.fromEntries(
    zip(
        Object.keys(obj[0]),
        zip(...obj.map(Object.values))
    ));


console.log(res)
georg
  • 211,518
  • 52
  • 313
  • 390
0

You could map your array to the entries of your object (using .map), and then use .forEach with .reduce() to build an object which has each key from your objects in your array with the values being arrays populated with their respective value:

const arr = [{name: 'parry1', age:22, height:5}, {name: 'parry 2', age:23, height:6}, {name: 'parry3', age:24, height:7}];

const res = arr.map(Object.entries).reduce((acc, entries) => {
  entries.forEach(([k, v]) => {
    acc[k] = [...(acc[k] || []), v];
  });
  return acc;
}, {});

console.log(res);
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
0

You could take the entries and return an object with all keys as arrays with their values.

var array = [{ name: 'parry1', age: 22, height: 5 }, { name: 'parry 2', age: 23, height: 6 }, { name: 'parry3', age: 24, height: 7 }],
    result = array.reduce((r, o) => Object.entries(o).reduce((q, [k, v]) => {
        q[k] = q[k] || [];
        q[k].push(v);
        return q;
    }, r), {});

console.log(result);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

Try this.

const obj = [
  {name: 'parry1', age:22, height:5},
  {name: 'parry 2', age:23, height:6},
  {name: 'parry3', age:24, height:7}
]

let output = {};

obj.forEach((o) => {
  Object.keys(o).forEach((prop) => {
    if (output[prop] === undefined) {
      output[prop] = [];
    }

    output[prop].push(o[prop]);
  });
})

console.log(output);
Aditya Bhave
  • 998
  • 1
  • 5
  • 10
0

Loop through the original array. For each row, check if there is a corresponding field in the resObj. If no, then create the field. Push the value into this field.

Code -

const obj = [
  {name: 'parry1', age:22, height:5},
  {name: 'parry 2', age:23, height:6},
  {name: 'parry3', age:24, height:7}
]

function arrayToObj(arr) {
  let resObj={};
  arr.forEach(row=> {
    Object.keys(row).forEach(key=>{
      if(!resObj[key]) {
        resObj[key]=[];
      }
      resObj[key].push(row[key]);
    })
  })
  return resObj;
}

console.log(JSON.stringify(arrayToObj(obj)));
// {"name":["parry1","parry 2","parry3"],"age":[22,23,24],"height":[5,6,7]}
Nithin Kumar Biliya
  • 2,763
  • 3
  • 34
  • 54