两个数组如何匹配合并成

发布于 2022-09-13 01:26:27 字数 1466 浏览 13 评论 0

      cameraTypeList: [
        { title: "道路沿线", bg: "dlyx-btn", type: "1" },
        { title: "桥梁", bg: "mj-btn", type: "2" },
        { title: "隧道", bg: "qd-btn", type: "3" },
        { title: "ETC门架", bg: "etc-btn", type: "7" },
        { title: "收费站", bg: "sfz-btn", type: "5" },
        { title: "服务区", bg: "fwq-btn", type: "6" },
        { title: "加油站", bg: "online-btn", type: "9" },
        { title: "其它", bg: "qt-btn", type: "4" }, 
      ],
      cameraClassifyCount:[
        {  classify: "1" ,count:"12344"},
        {  classify: "2" ,count:"2321"},
        {  classify: "3" ,count:"333"},
        {  classify: "7" ,count:"123"},
        {  classify: "5" ,count:"444"},
        {  classify: "6" ,count:"666"},
        {  classify: "9" ,count:"111"},
        {  classify: "4" ,count:"222"}, 
      ],

根据 type = classify 想把 count 数据匹配加到第一个数组里。

cameraTypeList: [
        { title: "道路沿线", bg: "dlyx-btn", type: "1" , count:"12344" },
        { title: "桥梁", bg: "mj-btn", type: "2" , count:"2321" },
        { title: "隧道", bg: "qd-btn", type: "3" , count:"333" },
        { title: "ETC门架", bg: "etc-btn", type: "7" , count:"123" },
        { title: "收费站", bg: "sfz-btn", type: "5" , count:"444" },
        { title: "服务区", bg: "fwq-btn", type: "6" , count:"666" },
        { title: "加油站", bg: "online-btn", type: "9" , count:"111" },
        { title: "其它", bg: "qt-btn", type: "4" , count:"222" }, 
      ],

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

窝囊感情。 2022-09-20 01:26:27
let obj = {cameraTypeList: [
        { title: "道路沿线", bg: "dlyx-btn", type: "1" },
        { title: "桥梁", bg: "mj-btn", type: "2" },
        { title: "隧道", bg: "qd-btn", type: "3" },
        { title: "ETC门架", bg: "etc-btn", type: "7" },
        { title: "收费站", bg: "sfz-btn", type: "5" },
        { title: "服务区", bg: "fwq-btn", type: "6" },
        { title: "加油站", bg: "online-btn", type: "9" },
        { title: "其它", bg: "qt-btn", type: "4" }, 
      ],
      cameraClassifyCount:[
        {  classify: "1" ,count:"12344"},
        {  classify: "2" ,count:"2321"},
        {  classify: "3" ,count:"333"},
        {  classify: "7" ,count:"123"},
        {  classify: "5" ,count:"444"},
        {  classify: "6" ,count:"666"},
        {  classify: "9" ,count:"111"},
        {  classify: "4" ,count:"222"}, 
      ]};

let classifyCountMap = obj.cameraClassifyCount.reduce((acc,{classify,count})=>(acc[classify] = count,acc),{});
//{1: '12344', 2: '2321', 3: '333', 4: '222', 5: '444', 6: '666', 7: '123', 9: '111'}
// 合并到新数组
obj.cameraTypeList.map(item=>({...item,count:classifyCountMap[item.type]}));
// 修改原数组
obj.cameraTypeList.forEach(item=>item.count = classifyCountMap[item.type]);
console.log(obj.cameraTypeList);

image.png

阳光下慵懒的猫 2022-09-20 01:26:27
const cameraTypeList = [
      { title: "道路沿线", bg: "dlyx-btn", type: "1" },
      { title: "桥梁", bg: "mj-btn", type: "2" },
      { title: "隧道", bg: "qd-btn", type: "3" },
      { title: "ETC门架", bg: "etc-btn", type: "7" },
      { title: "收费站", bg: "sfz-btn", type: "5" },
      { title: "服务区", bg: "fwq-btn", type: "6" },
      { title: "加油站", bg: "online-btn", type: "9" },
      { title: "其它", bg: "qt-btn", type: "4" }
    ];
    const cameraClassifyCount = [
      { classify: "1", count: "12344" },
      { classify: "2", count: "2321" },
      { classify: "3", count: "333" },
      { classify: "7", count: "123" },
      { classify: "5", count: "444" },
      { classify: "6", count: "666" },
      { classify: "9", count: "111" },
      { classify: "4", count: "222" }
    ];
    const res = cameraTypeList.map(item => {
      const { count = 0 } = cameraClassifyCount.find(r => r.classify === item.type) || {};
      return { ...item, count };
    });
    console.log(res);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文