let specGroup = [
// [{key: "stock", value: 88}]
]
let goods = {
id: 1,
name: "荣耀手机",
categoryId: 1,
images: [''],
status: 1,
specList: [
{id: 1, name: "颜色", params: "color", values: [
{id: 1, spec_id: 1, spec_value: "红色"},
{id: 3, spec_id: 1, spec_value: "黑色"}
]},
{id: 2, name: "存储容量", params: "ram", values: [
{id: 2, spec_id: 2, spec_value: "8G"},
{id: 5, spec_id: 2, spec_value: "16G"}
]},
{id: 6, name: "版本", params: "version", values: [
{id: 15, spec_id: 6, spec_value: "官网标配"}
]},
{id: 7, name: "型号", params: "model", values: [
{id: 6, spec_id: 7, spec_value: "A号"},
{id: 7, spec_id: 7, spec_value: "B号"},
{id: 8, spec_id: 7, spec_value: "C号"},
{id: 9, spec_id: 7, spec_value: "D号"},
{id: 10, spec_id: 7, spec_value: "E号"},
{id: 11, spec_id: 7, spec_value: "F号"},
{id: 12, spec_id: 7, spec_value: "G号"},
{id: 13, spec_id: 7, spec_value: "H号"},
{id: 14, spec_id: 7, spec_value: "I号"}
]},
],
skuList: [
{
id: 2259, // skuId
spec_value_ids: [1,2],
spec_value_key: '1_2',
price: 100, // 价格(单位分)
stock: 110 // 当前 sku 组合对应的库存
},
{
id: 2260, // skuId
spec_value_ids: [1,5],
spec_value_key: '1_5',
price: 100, // 价格(单位分)
stock: 0 // 当前 sku 组合对应的库存
},
{
id: 2260, // skuId
spec_value_ids: [3,2],
spec_value_key: '3_2',
price: 100, // 价格(单位分)
stock: 10 // 当前 sku 组合对应的库存
},
{
id: 2260, // skuId
spec_value_ids: [3,5],
spec_value_key: '3_5',
price: 100, // 价格(单位分)
stock: 110 // 当前 sku 组合对应的库存
}
]
}
let specNames = {};
let specValues = {};
goods.specList.map((item, index) => {
specNames[item.id] = {
id: item.id,
label: item.name,
prop: item.params
}
item.values.forEach((v) => {
specValues[v.id] = v
})
})
function groupBy(array, f) {
const groups = {}
array.forEach(o => {
const group = JSON.stringify(f(o))
groups[group] = groups[group] || []
groups[group].push(o)
})
return Object.keys(groups).map(group => {
return groups[group]
})
}
function arrayGroupBy(list, groupId) {
return this.groupBy(list, item => {
return [item[groupId]]
})
}
// 选中的规格
let checkedSpec = [
{id: 1, spec_id: 1, spec_value: "红色"},
{id: 2, spec_id: 2, spec_value: "8G"}
]
// if (specGroup.length > 0 ) {
// }
// 选中的规格分下组
specGroup = arrayGroupBy(checkedSpec, 'spec_id')
// 组合算法
let combination = specGroup.reduce((previous, current) => {
let arr = [];
previous.forEach(previous => {
current.forEach(item => {
let combination = previous.concat(item)
arr.push(combination)
})
})
return arr;
}, [[]])
if (combination[0].length > 0) {
// 重新处理下生成的sku
combination.forEach((itemArr, index) => {
let obj = {}
let ids = []
itemArr.forEach((v)=>{
ids.push(v.id)
})
obj.spec_value_key = ids.join('_')
obj.spec_value_ids = ids
obj.stock = 0
obj.price = 0
// 替换sku中的元素(如果找到)
if (goods.skuList.length === 0 ) {
goods.skuList.push(obj)
} else {
let skuListIndex = goods.skuList.findIndex((sku, index) => {
return sku.spec_value_key === obj.spec_value_key
})
if (skuListIndex === -1) {
goods.skuList.push(obj)
} else {
goods.skuList.splice(skuListIndex, 1, obj)
}
}
})
}
// 表格列头、skuData
let skuColumns = [];
let skuData = [];
goods.skuList.forEach((item, index) => {
item.spec_value_ids.forEach((v) => {
let specVal = specValues[v];
let specName = specNames[specVal.spec_id];
let key = specNames[specVal.spec_id].prop
let value = specVal.spec_value
item[key] = value
// 生成列头
if (index === 0) {
skuColumns.push(specName)
}
})
})
let prevKey = [1,5,9];
let currKey = [1,5,4,9];
let res = false;
let difference = prevKey.filter(function(v){ return currKey.indexOf(v) === -1 })
console.log(difference)
console