我有一个像这样的数组:
var my_array= [
[2, [[9, 10]]],
[5, [[10, 11]]],
[4, [[11, 9]]],
[1, [[19, 2], [41, 10]]],
[7, [[17, 3]]],
[0, [[11, 4], [18, 5]]]
]
中的数组my_array
包含另外两个数组(第一个不是必需的,但要看第二个(myarray[1]
:此数组包含不同的x / y坐标[18, 4]
)
我想得到另一个看起来像这样的数组(下面的解释):
var result_array= [
[ [2, [9, 10]], [5, [10, 11]], [4, [11, 9]], [0, [11, 4]] ],
[ [1, [19, 2]], [0, [18, 5]] , [7, [17, 3]] ],
[ [1, [41, 10]] ]
]
数组现在按其x值([9, 10]
-> x-value :)进行排序,9
并分组为新数组。的x值之间的差可以是,比方说,+/- 2个索引(x值7,8,9,10,11)可以在一个组中。
我不知道该如何编码。这是我到目前为止所拥有的:
var my_array= [
[2, [[9, 10]]],
[5, [[10, 11]]],
[4, [[11, 9]]],
[1, [[19, 2], [41, 10]]],
[7, [[17, 3]]],
[0, [[11, 4], [18, 5]]]
]
function sortArray(array) {
var difference = 2,
result = '';
var array_sorted = [];
array.forEach(function(a) {
a[1].forEach(function(b) {
array_sorted.push([b[0],b[1],a[0]]);
})
})
array_sorted = array_sorted.sort(function(a,b) {return a[0]-b[0]});
array_sorted.forEach(function(a) {
if (a[0] > difference) {
difference = a[0];
array_sorted.push(array_group); array_group = [];}
array_group.push([a]);
})
return array_sorted;
}
console.log(sortArray(my_array));
编辑:我忘记提及的一点是y-value difference
,应分组的坐标的不应大于1
。看下面的例子:
(x: 3, y:1),(x: 1, y:2),(x: 2, y:3),(x: 4, y:4)
-> not (x: 4, y:41)
编辑2:
var my_array= [
[2, [[9, 10]]],
[5, [[10, 11]]],
[4, [[11, 9]]],
[1, [[19, 2], [41, 10]]],
[7, [[17, 3]]],
[0, [[11, 4], [18, 5]]]
]
var result_array= [
[ [2, [9, 10]], [5, [10, 11]], [4, [11, 9]] ], // line 1
[ [0, [11, 4]] ], // line 2
[ [1, [19, 2]] ], // line 3
[ [7, [17, 3]] ], // line 4
[ [0, [18, 5]] ], // line 5
[ [1, [41, 10]] ] // line 6
]
如果您看一下第1行和第2行,则x值(第2行:'11'和第1行:'9','10','9'会完美匹配在一起。现在,我也想将y值就像我在上面编辑的示例中一样->即使the x-values match together
,也应该将它们分组为新数组y-values match together
。
Y值匹配表示存在一行->
(x:2,y:4),(x:1,y:5),(x:2,y:6),(x:2 ,y:7)而不是(x:4,y:42)之类的值
我希望我的编辑可以使我的想法更容易理解。
预先感谢,乔纳斯
编辑:格式和代码样式
Edit2:对原始问题编辑的回复
var my_array= [
[2, [[9, 10]]],
[5, [[10, 11]]],
[4, [[11, 9]]],
[1, [[19, 2], [41, 10]]],
[7, [[17, 3]]],
[0, [[11, 4], [18, 5]]]
]
var xdifference = 2;
var ydifference = 1;
function split(input_array) {
var splitted = [];
input_array.forEach(function (item) {
var coordinates = item[1];
coordinates.forEach(function (coordinate) {
splitted.push([item[0], coordinate]);
});
});
return splitted;
}
function getXValueOf(item) {
return item[1][0];
}
function getYValueOf(item) {
return item[1][1];
}
function divideIntoHeaps(sorted_array) {
var heaps = [];
function findMatchingHeap(item) {
var matching = heaps.find(function (heap) {
return heap.every(function (itemOfHeap) {
var xMatches = Math.abs(getXValueOf(item) - getXValueOf(itemOfHeap)) <= xdifference+1;
var yMatches = Math.abs(getYValueOf(item) - getYValueOf(itemOfHeap)) <= ydifference+1;
return xMatches && yMatches;
});
});
return matching;
}
function allocate(item) {
if (heaps.length == 0) {
heaps.push([item]);
} else {
var matchingHeap = findMatchingHeap(item);
if (matchingHeap !== undefined) {
matchingHeap.push(item);
} else {
heaps.push([item]);
}
}
}
sorted_array.forEach(allocate);
return heaps;
}
function sortArray(my_array) {
var splitted = split(my_array);
var result = divideIntoHeaps(splitted);
return result;
}
var result = sortArray(my_array);
result.forEach( function (row) {
console.log(JSON.stringify(row));
});
.as-console-wrapper { max-height: 100% !important; top: 0; }
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句