-
Notifications
You must be signed in to change notification settings - Fork 0
/
33、全组合判断.js
54 lines (49 loc) · 1.72 KB
/
33、全组合判断.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
// 笛卡尔积算法
function isFullCombination(datas) {
if (datas.length === 0) return false;
const fieldMap = new Map(); // 字段映射对象
const keys = Object.keys(datas[0]);
const valueMap = new Map();
let n = 1;
const combinationsSet = new Set();
for (const item of datas) {
let combination = '';
for (const key of keys) {
const value = item[key];
let valueSet = fieldMap.get(key);
if (!valueSet) {
valueSet = new Set();
fieldMap.set(key, valueSet);
}
valueSet.add(value);
let num = valueMap.get(value);
if (!num) {
num = n++;
valueMap.set(value, num);
}
combination += `-${num}`;
}
if (combinationsSet.has(combination)) {
return false;
}
combinationsSet.add(combination)
}
const totalLength = [...fieldMap].reduce((a, [, v]) => (a *= v.size), 1)
console.log(totalLength);
return totalLength === datas.length;
}
const inputData = [
{ 字段1: '甲', 字段2: 'a', 字段3: 1 },
{ 字段1: '甲', 字段2: 'a', 字段3: 2 },
{ 字段1: '甲', 字段2: 'a', 字段3: 3 },
{ 字段1: '甲', 字段2: 'b', 字段3: 1 },
{ 字段1: '甲', 字段2: 'b', 字段3: 2 },
{ 字段1: '甲', 字段2: 'b', 字段3: 3 },
{ 字段1: '乙', 字段2: 'a', 字段3: 1 },
{ 字段1: '乙', 字段2: 'a', 字段3: 2 },
{ 字段1: '乙', 字段2: 'a', 字段3: 3 },
{ 字段1: '乙', 字段2: 'b', 字段3: 1 },
{ 字段1: '乙', 字段2: 'b', 字段3: 2 },
{ 字段1: '乙', 字段2: 'b', 字段3: 3 },
]
console.log(isFullCombination(inputData))