1

I have an array of objects that looks like this:

arr = [
  {name: "john", age: 23},
  {name: "mary", age: 40},
  {name: "zack", age: 17}
]

I am trying to convert it into something like this:

{
name: ["john", "mary", "zack"],
age: ['23', 40, 17]
}

i have tried the following

arr.map(item => item.name)
arr.map(item => item.age)
return {names, ages}

and it works fine but this assumes that you already know, beforehand, the keys of the objects you're converting.

I want to be able to load the object keys and corresponding array of values dynamically. Assuming i don't know that the objects in our example array have "name" and "age" as keys.

0

6 Answers 6

2

You could reduce the array and the entries of the object and collect the values in the group of the keys.

const 
    data = [{ name: "john", age: 23 }, { name: "mary", age: 40 }, { name: "zack", age: 17 }],
    result = data.reduce((r, o) => Object.entries(o).reduce((t, [k, v]) => {
        if (!t[k]) t[k] = [];
        t[k].push(v);
        return t;
    }, r), {});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

Comments

0

You could get the key of the first element and then map through it. With each, get its corresponded values

const arr = [
  { name: "john", age: 23, gender: "male" },
  { name: "mary", age: 40, gender: "female" },
  { name: "zack", age: 17, gender: "male" },
]

const res = Object.keys(arr[0]).reduce((acc, el) => {
  const values = arr.map((item) => item[el])
  return { ...acc, [el]: values }
}, {})

console.log(res)

1 Comment

Just one thing with this approach is like, for some reason if the first object is not having some of the keys(for eg., consider gender is not present in arr[0]), then it might not be present in the output at all.
0

Assuming that each object in your list has the same keys you could get the keys of the first object

const keys = Object.keys(arr[0])

and then map through the keys with your above approach

const returnObj = {}
keys.forEach(key => {
   returnObj[key] = arr.map(item => item[key])
})
return returnObj

Comments

0

You can use Object.entries for the mapping.

var arr = [
  {name: "john", age: 23},
  {name: "mary", age: 40},
  {name: "zack", age: 17}
];

var entries = arr.map((item) => Object.entries(item));

var result = {};
entries.forEach(entry => {
  entry.forEach(item => {
    if (result[item[0]] && result[item[0]].length > 0) {
      result[item[0]].push(item[1]);
    } else {
        result[item[0]] = [item[1]];
    }
  });
});
console.log(result);

Comments

0

You can make use of Array.reduce and Object.keys.

let arr = [
  {name: "john", age: 23},
  {name: "mary", age: 40},
  {name: "zack", age: 17}
]

const formatData = (data) => {
  return data.reduce((res, obj) => {
    Object.keys(obj).map(d => {
      res[d] = [...(res[d] ||[]), obj[d]]
    })
    return res;
  }, {})
}

console.log(formatData(arr))

Comments

0

You can do this with Ramda

import { mergeWith, concat } from “Ramda”

const mergeConcat = mergeWith(concat)

mergeConcat(arr)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.