javascript sku数据格式如何集合?

const arrygoods = { "goodsSpu": { "id": 7, "goods_name": "商品测试4", "low_price": "12.00", "cover": "https://easyswoole-1258572605.cos.ap-shanghai.myqcloud.com/20191029191943tN0byPtimg%20%285%29.jpg", "image": "[\"https:\\/\\/easyswoole-1258572605.cos.ap-shanghai.myqcloud.com\\/20191029192012bAONEdtimg%20%287%29.jpg\",\"https:\\/\\/easyswoole-1258572605.cos.ap-shanghai.myqcloud.com\\/20191029192015mczQHktimg%20%289%29.jpg\"]", "content": "<p>这是图文内容。。。。</p><p><img src=\"https://easyswoole-1258572605.cos.ap-shanghai.myqcloud.com/20191028225309Dh1feotimg%20%285%29.jpg\" alt=\"undefined\"><br></p>", "total_inventory": 20, "brand_name": "默认品牌2", "category_name": "默认分类2" }, "goodsSku": [{ "sku_id": 9, "sku_name": "小黑", "price": "12.00", "stock": 0, "parent_rate": "0.0100", "ancestor_rate": "0.0100", "specList": [{ "颜色": "黑色" }, { "尺寸": "小号" }] }, { "sku_id": 10, "sku_name": "小白", "price": "15.00", "stock": 0, "parent_rate": "0.0100", "ancestor_rate": "0.0200", "specList": [{ "颜色": "白色" }, { "尺寸": "小号" }] }] }

这是原始数据
比如两个数组中尺寸小号有重复的
从结果集俩面,读取不重复的数字,生成下面的这种

{ "颜色":["黑色", "白色"], "尺寸":["小号"]}

`const goodInfo = {

  colorList: [],
  typeList: []
};
for (const good of arrygoods.goodsSku) {
  for (const item of good.specList) {
    console.log(item);
    const color = item['颜色'];
    const type = item['尺寸'];
    if (color && goodInfo.colorList.indexOf(color) < 0) {
      goodInfo.colorList.push(color);
    } else if (type && goodInfo.typeList.indexOf(type) < 0) {
      goodInfo.typeList.push(type);
    }
  }
}`
阅读 1.8k
1 个回答
var goodsSku = [
  { "sku_id": 9, "sku_name": "小黑", "price": "12.00", "stock": 0, "parent_rate": "0.0100", "ancestor_rate": "0.0100", "specList": [{ color: "黑色" }, { size: "小号" }] },
  { "sku_id": 10, "sku_name": "小白", "price": "15.00", "stock": 0, "parent_rate": "0.0100", "ancestor_rate": "0.0200", "specList": [{ color: "白色" }, { size: "小号" }] }
]
const test = (goodsSku) => {
  let temp1 = []
  goodsSku.forEach(item=> {
    temp1.push(...item.specList)
  })

  return goodsSku.map(item=>{
    let json = {...item}
    if (temp1 && temp1.length > 0) {
      json.color = []
      json.size = []
      temp1.forEach(i => {
        i.color && json.color.push(i.color)
        i.size && json.size.push(i.size)
      })
    }
    return {
      ...json,
      color: [...new Set(json.color)],
      size: [...new Set(json.size)]
    }
  })
 
}
 console.log(test(goodsSku))
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题