2

let data = [
  {firstName: 'John', lastName: 'Doe'},
  {firstName: 'Mike', lastName: 'Smith'}
]

console.log(data)

I want to transform this into one object like this

obj = {firstName: ['John', 'Mike'], lastName: ['Doe', 'Smith']}

should I use reduce?

6 Answers 6

2

A generic solution with Array#reduce method.

let result = data.reduce((obj, o) => {
  // get all keys of object
  Object.keys(o)
    // iterate over the keys
    .forEach(k => {
      // define property if not defined
      obj[k] = obj[k] || [];
      // push the value to the array
      obj[k].push(o[k]);
    })
  // return object
  return obj;
  // set initial value as an object for result
}, {})

let data = [{
    firstName: 'John',
    lastName: 'Doe'
  },
  {
    firstName: 'Mike',
    lastName: 'Smith'
  }
]

let result = data.reduce((obj, o) => {
  Object.keys(o)
    .forEach(k => {
      obj[k] = obj[k] || [];
      obj[k].push(o[k]);
    })
  return obj;
}, {})

console.log(result)

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

Comments

2

You can use reduce to create a new version of the object.

The reduce() method executes a reducer function (that you provide) on each member of the array resulting in a single output value.

With reduce, we first pass in a function that executes on each item and returns a new output value, then we pass a second parameter defining the initial structure of the single output value.

let data = [
  {firstName: 'John', lastName: 'Doe'},
  {firstName: 'Mike', lastName: 'Smith'}
]

// o = the current output value
// i = the current item in the array
let result = data.reduce((o, i) => {
  // Add the first/last names to the corresponding array
  o.firstName.push(i.firstName)
  o.lastName.push(i.lastName)
  // Return the new current output value
  return o
}, { firstName: [], lastName: [] }) // Sets the initial output value

console.log(result)

Comments

1

let data = [
  {firstName: 'John', lastName: 'Doe'},
  {firstName: 'Mike', lastName: 'Smith'}
]

var firstName = [];var lastName = [];
data.forEach(function(item){
    firstName.push(item.firstName);
    lastName.push(item.lastName);
})

let obj = {};
obj.firstName = firstName;
obj.lastName = lastName;

let dataModified = [];
dataModified.push(obj);

console.log(dataModified);

Comments

1

Here's a single iteration, functional, non-mutating solution:

let data = [
  {firstName: 'John', lastName: 'Doe'},
  {firstName: 'Mike', lastName: 'Smith'}
]

let result = data.reduce((res, {firstName, lastName}) => ({
  firstName: [firstName, ...res.firstName],
  lastName:  [lastName,  ...res.lastName]
}), {firstName: [], lastName: []})

console.log(result)

Comments

0

You can also make the logic more generic, irrespective of the properties known

let data = [{
    firstName: 'John',
    lastName: 'Doe'
  },
  {
    firstName: 'Mike',
    lastName: 'Smith'
  }
]

let result = data.reduce((o, i) => {
  for (const key in i) {
    if (!o[key]) {
      o[key] = [];
    }
    o[key].push(i[key]);
  }
  return o
}, {})

console.log(result)

Comments

0

Another simple thing we can do is to call map two times, once on the first names, and once on the last names.

let data = [
  {firstName: 'John', lastName: 'Doe'},
  {firstName: 'Mike', lastName: 'Smith'}
]

let result = { 
  firstName: data.map(i => i.firstName), 
  lastName: data.map(i => i.lastName) 
}

console.log(result)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.