简体   繁体   中英

Copy objects from the nested array into the new array in React JS

I have a big nested array of objects and each object has an object called 'set. Something like this:

const arr1 = [
  {
    id: 1,
    subject: 'Subject',
    set: {
      title: 'Title',
      subTitle1: 'SubTitle',
      subTitle2: 'SubTitle',
    },
  },
  {
    id: 2,
    subject: 'Subject',
    set: {
      title: 'Title',
      subTitle1: 'SubTitle',
      subTitle2: 'SubTitle',
    },
  },
]

I want to get the content of each 'set' object from 'arr1' into the 'arr2', like this:

const arr2 = [
  {
    title: 'Title',
    subTitle1: 'SubTitle',
    subTitle2: 'SubTitle',
  },
  {
    title: 'Title',
    subTitle1: 'SubTitle',
    subTitle2: 'SubTitle',
  },
]

You can use .map to extract the set from each object

 const arr1 = [ { id: 1, subject: 'Subject', set: { title: 'Title', subTitle1: 'SubTitle', subTitle2: 'SubTitle', }, }, { id: 2, subject: 'Subject', set: { title: 'Title', subTitle1: 'SubTitle', subTitle2: 'SubTitle', }, }, ] const arr2 = arr1.map(a => a.set); console.log(arr2 );

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