简体   繁体   English

将嵌套数组中的对象复制到 React JS 中的新数组中

[英]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.我有一个很大的嵌套对象数组,每个对象都有一个名为“set.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:我想将每个“set”对象的内容从“arr1”获取到“arr2”中,如下所示:

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您可以使用.map从每个对象中提取集合

 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 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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