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);
}
}
}`