js库存数据组装?

`

// 根据attributes里specifications数组中的id跟skus里specificationIds做关联, 如果相等则将数据相乘添加到新的对象当中
const data = {
  attributes: [
    {
      id: '2851',
      name: '衣柜',
      specifications: [
        {
          attributeId: '2851',
          name: '黑色',
          id: '4685'
        },
        {
          attributeId: '2851',
          name: '白色',
          id: '4686'
        }
      ]
    },
    {
      id: '2852',
      name: '床',
      specifications: [
        {
          attributeId: '2852',
          goodsId: '1182246',
          name: '浅黑色',
          id: '4687'
        },
        {
          attributeId: '2852',
          goodsId: '1182246',
          name: '浅蓝色',
          id: '4688'
        }
      ]
    }
  ],
  skus: [
    {
      deleted: '0',
      goodsId: '1182246',
      id: '3676',
      price: '1288',
      skuSn: '3',
      specificationIds: ['4685', '4686'], // 属性值ID,如上面衣柜属性的黑色
      stock: '0'
    },
    {
      goodsId: '1182246',
      id: '3677',
      price: '1588',
      skuSn: '6',
      specificationIds: ['4687', '4688'],
      stock: '0'
    }
  ]
}
  }

// 实际想要得到的效果
const sku = [

{
  // 根据id匹配, 将attributes中的属性添加到sku里, skus的price和skuSn一一对应
  childProductSpec: {
    床: "浅黑色",
    衣柜: "黑色"
  },
  isUse: true,
  price: '1288',
  skuSn: '3'
},
{
  childProductSpec: {
    床: "浅蓝色",
    衣柜: "黑色"
  },
  isUse: true,
  price: '1288',
  skuSn: '6'
},
{
  childProductSpec: {
    床: "浅黑色",
    衣柜: "白色"
  },
  isUse: true,
  price: '1588',
  skuSn: '6'
},
{
  childProductSpec: {
    床: "浅蓝色",
    衣柜: "白色"
  },
  isUse: true,
  price: '1588',
  skuSn: '6'
}

]
`

// 这样操作得到的数据不符, 数据重复

`

  for (let i = 1; i < attributes.length; i++) {
    this.value = attributes[i].name
  }
  
  for (const j in list) {
    for (let k = 0; k < attributes.length; k++) {
      for (let m = 0; m < attributes[k].specifications.length; m++) {
        this.name = attributes[k].specifications[m].name
      }
    }
  }
  
  for (let k = 0; k < skus.length; k++) {
    const data = {
      childProductId: 0,
      childProductSpec: {
        // 颜色: '白色'
      },
      skuSn: skus[k].skuSn,
      childProductStock: 0,
      childProductPrice: 0,
      price: skus[k].price,
      isUse: true
    }

    data.childProductSpec[this.value] = this.name
    skusData.push(data)
    }
  }

`

// 上面处理之后的数据, 数据有重复
  skusData = [
    {
      childProductId: 0,
      childProductPrice: 0,
      childProductSpec: {
        床: '浅黑色',
      },
      childProductStock: 0,
      isUse: true,
      price: '1588',
      skuSn: '6'
    },
    {
      childProductId: 0,
      childProductPrice: 0,
      childProductSpec: {
        床: '浅黑色',
      },
      childProductStock: 0,
      isUse: true,
      price: '1588',
      skuSn: '6'
    }
  ]


阅读 1.7k
1 个回答

你这个源码不全啊。 跑不起来 我就自己写了个方法

用个lodash库

let result = [];
data.attributes.forEach(attribute => {
  attribute.specifications.forEach(specification => {
    const sku = _.find(data.skus, sku => sku.specificationIds.indexOf(specification.attributeId));
    if(sku){
      result.push({
        childProductSpec: {
          [attribute.name]: specification.name
        },
        isUse: true,
        price: sku.price,
        skuSn: sku.skuSn
      },)
    }else{
      // specifications 和 specificationIds不相等的情况

      // dosomething...
    }
  })
})
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题