You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何提取products数组零号元素的la、ld键值对放入上层dop对象

方案1:直接修改原数组

直接遍历修改原始数组,适合不需要保留原始数据的场景:

arr.forEach(item => {
  const targetDopItem = item.dop[0]
  const { ld, la } = targetDopItem.products[0]
  // 直接赋值到和c同级的位置
  targetDopItem.ld = ld
  targetDopItem.la = la
})

方案2:生成新数组(不修改原数据)

纯函数写法,不会污染原始数组,适合需要保留原数据的场景:

const processedArr = arr.map(rootItem => {
  return {
    ...rootItem,
    dop: rootItem.dop.map((dopItem, idx) => {
      // 仅处理包含c字段的dop项,如果你能确定是第一个元素也可以直接判断idx === 0
      if (!('c' in dopItem)) return dopItem
      const { ld, la } = dopItem.products[0]
      return {
        ...dopItem,
        ld,
        la
      }
    })
  }
})

处理完成后,dop数组内对应项的结构如下:

{
  c: 3,
  la: 33,
  ld: 11,
  products: [
    { ld: 11, la: 33 },
    { ld: 233, la: 44 }
  ]
}

内容的提问来源于stack exchange,提问作者Faizal Munna

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 11:24:05