简体   繁体   中英

How to convert an array of objects into array of arrays

I have this array of objects and I would like to convert it into array of arrays.

const data = [
    {
      id: 1,
      name: ‘Beetlejuice’,
      year: ‘1988’,
      phone: ‘242342342’
    },
    {
      id: 1,
      name: ‘Beetlejuice’,
      year: ‘1988’,
      phone: ‘242342342’
    },
    {
      id: 1,
      name: ‘Beetlejuice’,
      year: ‘1988’,
      phone: ‘242342342’
    },
    {
      id: 1,
      name: ‘Beetlejuice’,
      year: ‘1988’,
      phone: ‘242342342’
    },
  ];

Object.values with Array.map will help to create a new array with each node as the values of each object in the parrent array.

Logic

  • Run Array.map on the input array
  • On each object in array return Object.values of each node of object
  • This will give array of arrays where the inner array is tehe values of object in input array.

 const data = [{ id: 1, name: 'Beetlejuice', year: '1988', phone: '242342342' }, { id: 1, name: 'Beetlejuice', year: '1988', phone: '242342342' }, { id: 1, name: 'Beetlejuice', year: '1988', phone: '242342342' }, { id: 1, name: 'Beetlejuice', year: '1988', phone: '242342342' }]; const output = data.map(item => Object.values(item)); console.log(output);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM