账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    如何处理JavaScript笛卡尔积层级?
    • 2019-10-09 00:00
    • 10
    42
    0

    // 根据attributes的name及specifications中的name生成不同的规格, 用specifications的id和skus的specifications做关联, 把关联的price、stock、id一一匹配

    const datas = {
        attributes: [
          {
            id: '2895',
            goodsId: '1182256',
            name: '颜色',
            addTime: '2019-10-08 17:26:48',
            specifications: [
              {
                id: '4988',
                goodsId: '1182256',
                attributeId: '2895',
                name: '白色'
              },
              {
                id: '4989',
                goodsId: '1182256',
                attributeId: '2895',
                name: '黑色'
              }
            ]
          },
          {
            id: '2896',
            goodsId: '1182256',
            name: '尺寸',
            addTime: '2019-10-08 17:26:48',
            specifications: [
              {
                id: '4990',
                goodsId: '1182256',
                attributeId: '2896',
                name: 'L'
              },
              {
                id: '4991',
                goodsId: '1182256',
                attributeId: '2896',
                name: 'M'
              }
            ]
          }
        ],
        skus: [
          {
            id: '4959',
            goodsId: '1182256',
            price: '6.00',
            stock: '1000',
            addTime: '2019-10-08 17:26:48',
            deleted: '0',
            specificationIds: [
              '4988',
              '4990'
            ],
            skuSn: ''
          },
          {
            id: '4960',
            goodsId: '1182256',
            price: '7.00',
            stock: '1001',
            addTime: '2019-10-08 17:26:48',
            deleted: '0',
            specificationIds: [
              '4988',
              '4991'
            ],
            skuSn: ''
          },
          {
            id: '4961',
            goodsId: '1182256',
            price: '8.00',
            stock: '1002',
            addTime: '2019-10-08 17:26:48',
            deleted: '0',
            specificationIds: [
              '4989',
              '4990'
            ],
            skuSn: ''
          },
          {
            id: '4962',
            goodsId: '1182256',
            price: '9.00',
            stock: '1003',
            addTime: '2019-10-08 17:26:48',
            deleted: '0',
            specificationIds: [
              '4989',
              '4991'
            ],
            skuSn: ''
          }
        ]
      }

    // 需要把data的数据转换成SKU的数据

    const sku = [
        {
          childProductSpec: {
            颜色: '白色',
            尺寸: 'L'
          },
          price: '6.00',
          stock: '1000',
          id: '4959'
        },
        {
          childProductSpec: {
            颜色: '白色',
            尺寸: 'M'
          },
          price: '7.00',
          stock: '1001',
          id: '4960'
        },
        {
          childProductSpec: {
            颜色: '黑色',
            尺寸: 'L'
          },
          price: '8.00',
          stock: '1002',
          id: '4961'
        },
        {
          childProductSpec: {
            颜色: '黑色',
            尺寸: 'M'
          },
          price: '9.00',
          stock: '1003',
          id: '4962'
        }
      ]

    处理后的数据

      const result = []
      for (const [n, item] of attributes.entries()) { // 第一层
        if (n === 0) {
          for (const obj of item.specifications) { // 第一层的子层 specifications
            for (const [i, otherItem] of attributes.entries()) { // 除了第一层的其它层
              if (i !== 0) {
                for (const otherSp of otherItem.specifications) { // 除了第一层的其它层的子层
                  result.push(
                    {
                      childProductSpec: {
                        [item.name]: obj.name,
                        [otherItem.name]: otherSp.name
                      }
                    }
                  )
                }
              }
            }
          }
        }
      }

    上面处理之后的数据, 数据有重复, 没有把对应的id做关联

    skusData = [
        {
          childProductSpec: {
            颜色: '白色',
            尺寸: 'L'
          }
        },
        {
          childProductSpec: {
            颜色: '白色',
            尺寸: 'M'
          }
        },
        {
          childProductSpec: {
            颜色: '黑色',
            尺寸: 'L'
          }
        },
        {
          childProductSpec: {
            颜色: '黑色',
            尺寸: 'M'
          }
        }
      ]
    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 10 元积分
        全部回答
    • 0
    • 在JavaScript中,笛卡尔积(Cartesian Product)是一种集合的运算,它可以用来表示多个集合的交集。然而,对于多维数组或矩阵,由于它们的维度不同,不能直接使用笛卡尔积运算符。在这种情况下,你需要将它们转换为一种可以进行笛卡尔积运算的形式。

      在JavaScript中,可以使用reduce()方法和不同的方法来处理多维数组或矩阵。以下是一些示例:

      1. 使用reduce()方法:

      javascript let arrays = [[1, 2], [3, 4], [5, 6]]; let result = arrays.reduce((acc, cur) => { acc.push(cur[0] * cur[1]); return acc; }, []); console.log(result); // 输出:[24, 48, 72]

      在这个示例中,我们首先创建了一个二维数组。然后,我们使用reduce()方法和一个函数来计算每个元素的乘积。最后,我们将结果数组返回。

      1. 使用矩阵乘法:

      javascript let arrays = [[1, 2], [3, 4], [5, 6]]; let result = arrays.map((row, i) => { let col = row.map((val, j) => { return val * 10; }); return [[row[i], col[j]]]; }); console.log(result); // 输出:[[10, 20], [30, 40], [50, 60]]

      在这个示例中,我们首先创建了一个二维数组。然后,我们使用map()方法和一个函数来计算每个元素的乘积。最后,我们将结果数组返回。

      注意,以上两种方法都需要创建一个结果数组,而不是一个单独的矩阵。如果你不希望创建一个结果数组,你可以直接返回结果矩阵。例如:

      javascript let arrays = [[1, 2], [3, 4], [5, 6]]; let result = [[10, 20], [30, 40], [50, 60]]; console.log(result); // 输出:[[10, 20], [30, 40], [50, 60]]

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部