我在做工会调查。我希望根据其中一个索引是否与另一对索引共享一个数字来对数字对进行分组。所以:
我有一组对,如下所示:
pairs: [[1,3], [6,8], [3,8], [2,7]]有什么最好的方法来把他们组织成这样的工会:
[ [ 1, 3, 8, 6 ], [ 2, 7 ] ](1,3和3,8是因为他们共享3。这个群体与6,8团结在一起,因为他们共享8。用javascript做这件事的最好方法是什么?)
以下是其他例子:
pairs: [[8,5], [10,8], [4,18], [20,12], [5,2], [17,2], [13,25],[29,12], [22,2], [17,11]]
into [ [ 8, 5, 10, 2, 17, 22, 11 ],[ 4, 18 ],[ 20, 12, 29 ],[ 13, 25 ] ]编辑这里是我目前使用的方法:
findUnions = function(pairs, unions){
if (!unions){
unions = [pairs[0]];
pairs.shift();
}else{
if(pairs.length){
unions.push(pairs[0])
pairs.shift()
}
}
if (!pairs.length){
return unions
}
unite = true
while (unite && pairs.length){
unite = false
loop1:
for (i in unions){
loop2:
var length = pairs.length;
for (j=0;j<length;j++){
if (unions[i].includes(pairs[j][0])){
if (!unions[i].includes(pairs[j][1])){
unions[i].push(pairs[j][1])
pairs.splice(j, 1)
j-=1;
length-=1
unite = true
}else{
pairs.splice(j, 1)
j-=1
length-=1
}
}else if (unions[i].includes(pairs[j][1])){
unions[i].push(pairs[j][0])
pairs.splice(j, 1)
unite = true
j-=1
length-=1
}
}
}
}
return findUnions(pairs, unions)
}发布于 2017-07-26 00:16:50
方法:
finalArray = [], positions = {};
for i to Array.length
for j=i+1 to Array.length
find match between arr[i] and arr[j]
if match found
pos = postion mapped to either i or j in positions
add elements of arr[i] or arr[j] or both depending on pos.
return finalArray在这个方法中,我们继续存储数组的位置,我们在中添加到finalArray中,以后我们可以使用这个对象来找到一个合适的位置,在finalArray中添加匹配数组的元素。
function mergeArrays(finalArray, pos, subArray) {
for (var k = 0; k < subArray.length; k++) {
if (finalArray[pos].indexOf(subArray[k]) < 0)
finalArray[pos].push(subArray[k]);
}
}
function unionArrays(arr) {
var finalArray = [arr[0]],
positions = {
0: 0
};
for (var i = 0; i < arr.length; i++) {
for (var j = i + 1; j < arr.length; j++) {
for (var k = 0; k < arr[i].length; k++) {
if (arr[j].indexOf(arr[i][k]) >= 0) {
if (i in positions) {
mergeArrays(finalArray, positions[i], arr[j]);
positions[j] = positions[i];
} else if (j in positions) {
mergeArrays(finalArray, positions[j], arr[i]);
positions[i] = positions[j];
} else {
var pos = finalArray.length;
finalArray.push([]);
mergeArrays(finalArray, pos, arr[i]);
mergeArrays(finalArray, pos, arr[j]);
positions[i] = positions[j] = pos;
}
break;
}
}
}
if (!(i in positions)) {
finalArray.push(arr[i]);
positions[i] = finalArray.length - 1;
}
}
return finalArray;
}
console.log(unionArrays([[1,3], [6,8], [3,8], [2,7]]));
console.log(unionArrays([[8,5], [10,8], [4,18], [20,12], [5,2], [17,2], [13,25],[29,12], [22,2], [17,11]]));
发布于 2017-07-26 01:20:07
为了满足第一个需求,您可以迭代数组,在迭代过程中将当前数组从包含所有相邻索引的新数组中排除出来。检查相邻数组是否包含当前数组的一个或多个元素,是否真将元素推送到新数组。
为不包含先前筛选过的数组元素的元素筛选原始数组。
使用Set从数组中删除重复条目。
const arr = [[1,3], [6,8], [3,8], [2,7]];
let res = [];
for (const[key, [a, b]] of Object.entries(arr)) {
const adjacent = arr.filter((el, index) => index !== +key);
const has = adjacent.filter(el => el.includes(a) || el.includes(b));
res = [...res, ...has.filter(prop => !res.includes(prop))];
}
let not = new Set(...arr.filter(([a, b]) => !res.some(([c, d]) =>
a === c || b === d || a === d || b === c)));
let set = new Set();
for (const [a, b] of res) {
if (!set.has(a)) set.add(a);
if (!set.has(b)) set.add(b);
}
res = [[...set], [...not]];
console.log(res);
https://stackoverflow.com/questions/45315108
复制相似问题