例如:arr1 = [["26","27"],["28","27"],["22","21"]]输出结果为[["26","27","28"],["22","21"]]arr2 = [["26","27","28"],["26","27","28","29"],["22","21"]]输出结果为[["26","27","28","29"],["22","21"]]
该如何实现呢? 求教!
function trans(arr) { return arr.reduce((list, subList) => { const set = list.find(set => { return subList.some(num => set.has(num)) }) if(set) { subList.forEach(set.add.bind(set)) } else { list.push(new Set(subList)) } return list; }, []) .map(set => { return [...set] }) } console.log(trans([["26","27","28"],["26","27","28","29"]])) console.log(trans([["26","27"],["28","27"],["22","21"]]))
2.1m questions
2.1m answers
60 comments
57.0k users