function getObjKeys(obj) {
if (obj !== Object(obj)) {
throw new TypeError('Invalid object');
}
var keys = [];
for (var key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
keys[keys.length] = key;
}
}
return keys;
}
function arrayCombine(targetArr) {
var resultArr = [];
for (var n = 0; n <= targetArr.length; n++) {
var flagArrs = getFlagArrs(targetArr.length, n);
while (flagArrs.length) {
var flagArr = flagArrs.shift();
var combArr = Array(targetArr.length);
for (var i = 0; i < targetArr.length; i++) {
if (flagArr[i]) {
combArr[i] = targetArr[i];
}
}
resultArr.push(combArr);
}
}
return resultArr;
}
// 从m中取n的所有组合
function getFlagArrs(m, n) {
var flagArrs = [],
flagArr = [],
isEnd = false;
for (var i = 0; i < m; i++) {
flagArr[i] = i < n ? 1 : 0;
}
flagArrs.push(flagArr.concat());
// 当n不等于0并且m大于n的时候进入
if (n && m > n) {
while (!isEnd) {
var leftCnt = 0;
for (var i = 0; i < m - 1; i++) {
if (flagArr[i] == 1 && flagArr[i + 1] == 0) {
for (var j = 0; j < i; j++) {
flagArr[j] = j < leftCnt ? 1 : 0;
}
flagArr[i] = 0;
flagArr[i + 1] = 1;
var aTmp = flagArr.concat();
flagArrs.push(aTmp);
if (aTmp.slice(-n).join('').indexOf('0') == -1) {
isEnd = true;
}
break;
}
flagArr[i] == 1 && leftCnt++;
}
}
}
return flagArrs;
}
function initSKU(sku) {
var resultSKU = [];
var skuKeys = getObjKeys(sku);
for (var i in skuKeys) {
var skuKey = skuKeys[i]; // 获取一条SKU的key
var skuData = sku[skuKey]; // 获取一条SKU的相关数据
var skuKeyAttrs = skuKey.split(';'); // 获取SKU的key的属性数组
var combArr = arrayCombine(skuKeyAttrs);
for (var j = 0; j < combArr.length; j++) {
var key = combArr[j].join(';');
if (resultSKU[key]) {
resultSKU[key].count += skuData.count;
resultSKU[key].prices.push(skuData.price);
} else {
resultSKU[key] = {
count: skuData.count,
prices: [skuData.price]
};
}
}
}
return resultSKU;
}
var skuStockList = [{
"id": 171,
"productId": 35,
"skuCode": "202002250035001",
"price": 200,
"stock": 100,
"lowStock": 50,
"lockStock": 0,
"spData": "[{\"key\":\"颜色\",\"value\":\"红色\"},{\"key\":\"尺寸\",\"value\":\"38\"},{\"key\":\"风格\",\"value\":\"夏季\"}]"
},
{
"id": 172,
"productId": 35,
"skuCode": "202002250035002",
"price": 240,
"stock": 100,
"lowStock": 50,
"lockStock": 0,
"spData": "[{\"key\":\"颜色\",\"value\":\"红色\"},{\"key\":\"尺寸\",\"value\":\"38\"},{\"key\":\"风格\",\"value\":\"秋季\"}]"
},
{
"id": 173,
"productId": 35,
"skuCode": "202002250035003",
"price": 200,
"stock": 100,
"lowStock": 50,
"lockStock": 0,
"spData": "[{\"key\":\"颜色\",\"value\":\"红色\"},{\"key\":\"尺寸\",\"value\":\"39\"},{\"key\":\"风格\",\"value\":\"夏季\"}]"
},
{
"id": 174,
"productId": 35,
"skuCode": "202002250035004",
"price": 200,
"stock": 100,
"lowStock": 50,
"lockStock": 0,
"spData": "[{\"key\":\"颜色\",\"value\":\"红色\"},{\"key\":\"尺寸\",\"value\":\"39\"},{\"key\":\"风格\",\"value\":\"秋季\"}]"
},
{
"id": 175,
"productId": 35,
"skuCode": "202002250035005",
"price": 200,
"stock": 100,
"lowStock": 50,
"lockStock": 0,
"spData": "[{\"key\":\"颜色\",\"value\":\"蓝色\"},{\"key\":\"尺寸\",\"value\":\"38\"},{\"key\":\"风格\",\"value\":\"夏季\"}]"
},
{
"id": 176,
"productId": 35,
"skuCode": "202002250035006",
"price": 200,
"stock": 100,
"lowStock": 50,
"lockStock": 0,
"spData": "[{\"key\":\"颜色\",\"value\":\"蓝色\"},{\"key\":\"尺寸\",\"value\":\"38\"},{\"key\":\"风格\",\"value\":\"秋季\"}]"
},
{
"id": 177,
"productId": 35,
"skuCode": "202002250035007",
"price": 200,
"stock": 100,
"lowStock": 50,
"lockStock": 0,
"spData": "[{\"key\":\"颜色\",\"value\":\"蓝色\"},{\"key\":\"尺寸\",\"value\":\"39\"},{\"key\":\"风格\",\"value\":\"夏季\"}]"
},
{
"id": 178,
"productId": 35,
"skuCode": "202002250035008",
"price": 200,
"stock": 100,
"lowStock": 50,
"lockStock": 0,
"spData": "[{\"key\":\"颜色\",\"value\":\"蓝色\"},{\"key\":\"尺寸\",\"value\":\"39\"},{\"key\":\"风格\",\"value\":\"秋季\"}]"
}
]
/**
*
*
* /**
* 生成sku
* key = [
* {name: '颜色', item: ['红色', '蓝色'],price: 200 },
* {name: '尺寸', item: ['38', '39'],price: 250 },
* {name: '风格', item: ['春季', '夏季', '秋季', '冬季'],price: 200 },
* ]
* 数据集合
* sku = {
* '颜色;16G;电信': {price: 100, stock: 10}
* }
*/
*/
var skuArr = [];
var skuMap = {};
console