-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo.js
94 lines (89 loc) · 2.04 KB
/
demo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
function recurse(target, prefix) {
let texts = [];
let map = [];
for (const [key, value] of Object.entries(target)) {
const name = prefix ? prefix + "." + key : key;
if (typeof value === "object") {
const { texts: sTexts, map: sMaps } = recurse(value, name);
texts = texts.concat(sTexts);
map = map.concat(sMaps);
} else {
texts.push(value);
map.push({
name,
value,
});
}
}
return { texts, map };
}
const data = {
empty: true,
unknownSku: "1123",
actualSellerSku: "123123",
mappingSkus: [
{
id: "0.7575175723991803-id",
sku: "SKINTIFIC-02",
quantity: 2,
gift: false,
},
{
id: "0.7575175723991803-id",
sku: "SKINTIFIC-01",
quantity: 2,
gift: false,
},
{
id: "0.861613391585281-id",
sku: "SKINTIFIC-02",
quantity: 1,
gift: true,
},
{
id: "0.7593780708738294-id",
quantity: 3,
sku: "SKINTIFIC-03",
gift: true,
},
],
};
const nameList = (val, n) => {
const a = [];
a.length = n;
a.fill(val);
return a;
};
const handleSku = (list) => {
return list.reduce((cur, next) => {
const sku = next.sku.split("-");
cur[Number(sku[1])] = {
key: sku[1],
name: sku[0],
value: next.quantity,
};
return cur;
}, {});
};
const constStr = (list, prefix = false) => {
return list
.reduce((cur, next, idx) => {
const tmp = nameList(next.key, next.value);
cur.push(
idx === 0 && prefix ? [next.name, ...tmp].join("+") : tmp.join("+")
);
return cur;
}, [])
.join("+");
};
const skuList = data.mappingSkus.filter(
(cur) => cur.sku !== undefined && cur.quantity !== undefined
);
const normalSkuList = handleSku(skuList.filter((cur) => !cur.gift));
const giftSkuList = handleSku(skuList.filter((cur) => cur.gift));
console.log(normalSkuList, "---------", giftSkuList);
const ret = [
constStr(Object.values(normalSkuList), true),
`(${constStr(Object.values(giftSkuList))})`,
];
console.log(ret.join("+"));