数组与数组关键字段匹配,并插入?

作者站长头像
站长
· 阅读数 5

两个数组匹配关键字段,想将数组一插入到匹配数组二中。关键匹配字段是key 或者 dataIndex都可以。

let arrOne = [{
"ORDER_NUM": 58166870,
"ORDER_QTSB": 3,
"AREA_NAME": "总计",
"ORDER_GPON": 1451088,
"ORDER_PXJ": 10967,
"ORDER_DXGL": 1821963,
"ORDER_GXG": 94200,
"ORDER_OLT": 0,
"ORDER_CRAN": 2425,
"ORDER_TQ": 319835,
"ORDER_MSAP": 13141,
"ORDER_JTH": 57087,
"ORDER_PTGL": 54395997,
"ORDER_JZL": 164
}]

let arrTwo = [{
    "dataIndex": "ORDER_CRAN",
    "width": 200,
    "title": "设备1",
    "align": "center",
    "key": "ORDER_CRAN"
}, {
    "dataIndex": "ORDER_GPON",
    "width": 200,
    "title": "设备2",
    "align": "center",
    "key": "ORDER_GPON"
}, {
    "dataIndex": "ORDER_MSAP",
    "width": 200,
    "title": "设备3",
    "align": "center",
    "key": "ORDER_MSAP"
}, {
    "dataIndex": "ORDER_OLT",
    "width": 200,
    "title": "设备4",
    "align": "center",
    "key": "ORDER_OLT"
}, {
    "dataIndex": "ORDER_DXGL",
    "width": 200,
    "title": "设备5",
    "align": "center",
    "key": "ORDER_DXGL"
}, {
    "dataIndex": "ORDER_GXG",
    "width": 200,
    "title": "设备6",
    "align": "center",
    "key": "ORDER_GXG"
}, {
    "dataIndex": "ORDER_JZL",
    "width": 200,
    "title": "设备7",
    "align": "center",
    "key": "ORDER_JZL"
}, {
    "dataIndex": "ORDER_JTH",
    "width": 200,
    "title": "设备8",
    "align": "center",
    "key": "ORDER_JTH"
}, {
    "dataIndex": "ORDER_PXJ",
    "width": 200,
    "title": "设备9",
    "align": "center",
    "key": "ORDER_PXJ"
}, {
    "dataIndex": "ORDER_PTGL",
    "width": 200,
    "title": "设备10",
    "align": "center",
    "key": "ORDER_PTGL"
}, {
    "dataIndex": "ORDER_QTSB",
    "width": 200,
    "title": "设备11",
    "align": "center",
    "key": "ORDER_QTSB"
}, {
    "dataIndex": "ORDER_TQ",
    "width": 200,
    "title": "设备12",
    "align": "center",
    "key": "ORDER_TQ"
}]


希望得到的JSON

[{
  "dataIndex": "ORDER_CRAN",
  "ORDER_CRAN": 2425,    //从数组一匹配过来
  "width": 200,
  "title": "设备1",
  "align": "center",
  "key": "ORDER_CRAN"
},
  ...
]
回复
1个回答
avatar
test
2024-07-08
  for(let key in arrOne[0]) {
    for (let item of arrTwo) {
      if (item.dataIndex == key) {
        item[key] = arrOne[0][key]
        break
      }
    }
  }
回复
likes
适合作为回答的
  • 经过验证的有效解决办法
  • 自己的经验指引,对解决问题有帮助
  • 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
  • 询问内容细节或回复楼层
  • 与题目无关的内容
  • “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容