请问如何将二级嵌套数组合并成一级数组?

editDetails: [{
          causeName: '咳嗽001',
          children: [{
            productId: 7,
            proCode: 'CP_20201012001',
            proName: '产品1',
            proPrice: 10,
            number: 22,
            price: 222
          }, {
            productId: 7,
            proCode: 'CP_20201012001',
            proName: '产品1',
            proPrice: 10,
            number: 22,
            price: 222
          }],
          imageAttachment: '20201028014127383_oxxw29.jpg',
          videoAttachment: '20201028014131101_WeChat_20201027132303.mp4'
        }]
    
    
    如何将这个上面数组合并成下面这样
    
    editDetails: [{
        causeName: '咳嗽001',
        productId: 7,
        proCode: 'CP_20201012001',
        proName: '产品1',
        proPrice: 10,
        number: 22,
        price: 222,
        imageAttachment:'20201028014127383_oxxw29.jpg',
      videoAttachment: '20201028014131101_WeChat_20201027132303.mp4'
      }, {
        causeName: '咳嗽001',
        productId: 7,
        proCode: 'CP_20201012001',
        proName: '产品1',
        proPrice: 10,
        number: 22,
        price: 222,
        imageAttachment: '20201028014127383_oxxw29.jpg',
      videoAttachment: '20201028014131101_WeChat_20201027132303.mp4'
      }]
阅读 2.5k
3 个回答
data.reduce((res, item) => {
    const parent = {...item};
    delete parent.children;
    return res.concat(item.children.map(child => ({...parent, ...child}))
}, []);

const a = [
  {
    children: [{
      productId: 7,
      proCode: 'CP_20201012001',
      proName: '产品1',
      proPrice: 10,
      number: 22,
      price: 222
    }, {
      productId: 7,
      proCode: 'CP_20201012001',
      proName: '产品2',
      proPrice: 10,
      number: 22,
      price: 222
    }],
    causeName: '咳嗽001',
    imageAttachment: '20201028014127383_oxxw29.jpg',
    videoAttachment: '20201028014131101_WeChat_20201027132303.mp4'
  }
]

function simpleFlatten(data) {
  return data.reduce((acc, cur) => {
    const { children = [] } = cur
    const extras = Object.keys(cur).filter(i => i !== 'children').reduce((acc, key) => {
      return {
        ...acc,
        [key]: cur[key]
      }
    }, {})
    return [
      ...acc,
      ...children.map(i => ({
        ...i,
        ...extras
      }))
    ]
  }, [])
}

console.log(simpleFlatten(a))

我自己想的解决方案(有点傻),我是去循环拼接字符串

data.forEach(item => {
         item.children.forEach(items => {
           this.newArr.push(twoJsonMerge(items, {
            causeName: item.causeName,
             imageAttachment: item.imageAttachment,
             videoAttachment: item.videoAttachment
           }))
        })
      })
  
twoJsonMerge(json1, json2) {
  var length1 = 0; var length2 = 0; var jsonStr; var str
  for (var ever in json1) length1++
  for (var ever in json2) length2++
  if (length1 && length2)str = ','
  else str = ''
  jsonStr = ((JSON.stringify(json1)).replace(/,}/, '}') + (JSON.stringify(json2)).replace(/,}/, '}')).replace(/}{/, str)
  return JSON.parse(jsonStr)
}

第二种解决方案群里大佬写的

twoJsonMerge(originArry) {
  let res = []
  originArry.forEach(item => {
    const temp = item.children.map(_it => {
      return {
        ..._it,
        resName: `${item.resName}`,
        imageAttachment: `${item.imageAttachment}`,
        videoAttachment: `${item.videoAttachment}`
      }
    })
    res = [...res, ...temp]
  })
  return res
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏