js
function isFullCombination(datas) {
if (datas.length === 0) {
return false
}
const fieldMap = new Map() // 字段映射对象
const keys = Object.keys(datas[0])
const combinationSet = new Set()
const valueMap = new Map()
let n = 1
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 (combinationSet.has(combination)) {
return false
}
combinationSet.add(combination)
}
const n1 = [...fieldMap].reduce((s, [, v]) => (s *= v.size), 1)
const n2 = datas.length
return n1 === n2
}
const data1 = [
{ a: '-', b: '-' },
{ a: '-', b: '--' },
{ a: '--', b: '-' },
{ a: '--', b: '--' },
]
const datas = [
{ a: '甲', b: 'a', c: '1' },
{ a: '甲', b: 'a', c: '2' },
{ a: '甲', b: 'a', c: '3' },
{ a: '甲', b: 'b', c: '1' },
{ a: '甲', b: 'b', c: '2' },
{ a: '甲', b: 'b', c: '3' },
{ a: '乙', b: 'a', c: '1' },
{ a: '乙', b: 'a', c: '2' },
{ a: '乙', b: 'a', c: '3' },
{ a: '乙', b: 'b', c: '1' },
{ a: '乙', b: 'b', c: '2' },
{ a: '乙', b: 'b', c: '3' },
]
console.log('%c 🍒 isFullCombination(datas)', 'font-size:16px;color:#ea7e5c', isFullCombination(datas))