js操作两个一维数组转换为二维数组

本文介绍如何利用JavaScript的forEach和filter方法将两个一维数组转换为二维数组。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

js操作两个一维数组转换为二维数组

forEach 和 filter结合使用

     a.forEach((item,index)=>{
          item.children = b.filter(items=>{
              return items.goodsCategory === item.id;
          })
      })
      console.log(a);
let a = [
        {
          cagegoryName: "感冒药",
          createTime: "2020-06-19 14:44:51",
          delFlag: 1,
          id: "1273869332852719616",
          industryCategory: 2,
          level: 1,
          merchantId: "1239217264236613632",
          merchantUserId: "1273867462012780544",
          name: null,
          parentId: "0",
          sort: 1,
          storeId: "1273867968638566400",
          updateTime: "2020-06-19 14:44:51",
        },
        {
          cagegoryName: "消炎药",
          createTime: "2020-06-19 14:45:02",
          delFlag: 1,
          id: "1273869379375939584",
          industryCategory: 2,
          level: 1,
          merchantId: "1239217264236613632",
          merchantUserId: "1273867462012780544",
          name: null,
          parentId: "0",
          sort: 1,
          storeId: "1273867968638566400",
          updateTime: "2020-06-19 14:45:02",
        },
      ];

      let b = [
        {
          createTime: "2020-06-19 14:48:57",
          delFlag: 1,
          goodsCategory: "1273869332852719616",
          goodsCode: "123456",
          goodsName: "感冒灵颗粒",
          goodsPic: "1273870309353799680",
          goodsPrice: 9,
          id: "1273870363699396608",
          industryCategory: 2,
          isShelf: 1,
          itemNumber: "",
          merchantId: "1239217264236613632",
          merchantUserId: "1273867462012780544",
          name: null,
          saleRemark: null,
          salesNum: 13,
          sort: 1,
          source: 1,
          stockNum: 99986,
          storeId: "1273867968638566400",
          systemCategory: null,
          unit: "",
          updateTime: "2020-11-25 12:26:49",
        },
        {
          createTime: "2020-06-19 14:50:06",
          delFlag: 1,
          goodsCategory: "1273869332852719616",
          goodsCode: "123",
          goodsName: "白加黑",
          goodsPic: "1273870453247787008",
          goodsPrice: 10,
          id: "1273870656285655040",
          industryCategory: 2,
          isShelf: 1,
          itemNumber: "",
          merchantId: "1239217264236613632",
          merchantUserId: "1273867462012780544",
          name: null,
          saleRemark: null,
          salesNum: 2,
          sort: 1,
          source: 1,
          stockNum: 99997,
          storeId: "1273867968638566400",
          systemCategory: null,
          unit: "",
          updateTime: "2020-11-25 12:26:49",
        },
        {
          createTime: "2020-06-19 14:50:35",
          delFlag: 1,
          goodsCategory: "1273869332852719616",
          goodsCode: "1",
          goodsName: "双黄连口服液",
          goodsPic: "1273870731590189056",
          goodsPrice: 8,
          id: "1273870774388867072",
          industryCategory: 2,
          isShelf: 1,
          itemNumber: "",
          merchantId: "1239217264236613632",
          merchantUserId: "1273867462012780544",
          name: null,
          saleRemark: null,
          salesNum: 2,
          sort: 1,
          source: 1,
          stockNum: 99997,
          storeId: "1273867968638566400",
          systemCategory: null,
          unit: "",
          updateTime: "2020-11-25 12:26:49",
        },
        {
          createTime: "2020-06-19 14:51:11",
          delFlag: 1,
          goodsCategory: "1273869332852719616",
          goodsCode: "3",
          goodsName: "好感感冒药",
          goodsPic: "1273870847206178816",
          goodsPrice: 5,
          id: "1273870928152051712",
          industryCategory: 2,
          isShelf: 1,
          itemNumber: "",
          merchantId: "1239217264236613632",
          merchantUserId: "1273867462012780544",
          name: null,
          saleRemark: null,
          salesNum: 5,
          sort: 1,
          source: 1,
          stockNum: 99994,
          storeId: "1273867968638566400",
          systemCategory: null,
          unit: "",
          updateTime: "2020-11-25 12:26:49",
        },
        {
          createTime: "2020-06-19 14:54:56",
          delFlag: 1,
          goodsCategory: "1273869379375939584",
          goodsCode: "6",
          goodsName: "头孢克肟",
          goodsPic: "1273871831512858624",
          goodsPrice: 8,
          id: "1273871872604454912",
          industryCategory: 2,
          isShelf: 1,
          itemNumber: "",
          merchantId: "1239217264236613632",
          merchantUserId: "1273867462012780544",
          name: null,
          saleRemark: null,
          salesNum: 1,
          sort: 1,
          source: 1,
          stockNum: 99998,
          storeId: "1273867968638566400",
          systemCategory: null,
          unit: "",
          updateTime: "2020-11-21 18:05:12",
        },
        {
          createTime: "2020-06-19 14:55:25",
          delFlag: 1,
          goodsCategory: "1273869379375939584",
          goodsCode: "5",
          goodsName: "阿莫西林胶囊",
          goodsPic: "1273871948731072512",
          goodsPrice: 7,
          id: "1273871990716055552",
          industryCategory: 2,
          isShelf: 1,
          itemNumber: "",
          merchantId: "1239217264236613632",
          merchantUserId: "1273867462012780544",
          name: null,
          saleRemark: null,
          salesNum: 2,
          sort: 1,
          source: 1,
          stockNum: 99997,
          storeId: "1273867968638566400",
          systemCategory: null,
          unit: "",
          updateTime: "2020-11-21 17:47:27",
        },
        {
          createTime: "2020-06-19 14:55:57",
          delFlag: 1,
          goodsCategory: "1273869379375939584",
          goodsCode: "4",
          goodsName: "蒲地蓝消炎片",
          goodsPic: "1273872070277808128",
          goodsPrice: 5,
          id: "1273872125143498752",
          industryCategory: 2,
          isShelf: 1,
          itemNumber: "",
          merchantId: "1239217264236613632",
          merchantUserId: "1273867462012780544",
          name: null,
          saleRemark: null,
          salesNum: 1,
          sort: 1,
          source: 1,
          stockNum: 99998,
          storeId: "1273867968638566400",
          systemCategory: null,
          unit: "",
          updateTime: "2020-11-25 10:33:42",
        },
        {
          createTime: "2020-06-19 14:56:53",
          delFlag: 1,
          goodsCategory: "1273869379375939584",
          goodsCode: "2",
          goodsName: "盐酸左氧氟沙星胶囊",
          goodsPic: "1273872169963831296",
          goodsPrice: 6,
          id: "1273872362385915904",
          industryCategory: 2,
          isShelf: 1,
          itemNumber: "",
          merchantId: "1239217264236613632",
          merchantUserId: "1273867462012780544",
          name: null,
          saleRemark: null,
          salesNum: 2,
          sort: 1,
          source: 1,
          stockNum: 99997,
          storeId: "1273867968638566400",
          systemCategory: null,
          unit: "",
          updateTime: "2020-11-21 17:47:22",
        },
      ];
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值