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

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'
      }]

回答:

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
}

暂无评论

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注