我有一个二维数组,每个维度中有任意数量的元素,因此它是一个[m] [n]数组,但第二个维度长度(n
)是可变的。
第二维中的每个元素都包含一个数字,并且所有维中只存在一个数字。
因此,例如,数组可能是:
[
[
126,
131,
138,
139,
140,
143
],
[
126,
201
]
]
请注意,m
可以是> 2。
这是我的代码:
var theArray = [
[126, 131, 138, 139, 140, 143],
[126, 201]
];
for(var i = 0; i < theArray.length; i++) // loop through each array of numbers
{
$.each(theArray[i], function(index, value) // loop through all of the numbers in this array
{
var nextArray = (i+1<theArray.length?theArray[i+1]:theArray[0]);
if($.inArray(value, nextArray) == -1) // if this number is not in the next array
{
console.log("removing index: " + index + ", value: " + value);
theArray[i].splice(index, 1); // remove the number from the array
}
});
}
console.log(theArray);
输出是这样的:
removing index: 1, value: 131
removing index: 2, value: 139
removing index: 3, value: 143
removing index: 4, value: undefined
removing index: 5, value: undefined
removing index: 1, value: 201
Array
[
[
126,
138,
140,
],
[
126
]
]
JSFIDDLE:http://jsfiddle.net/hDL8K/
正如您所看到的,几乎有效,但无法删除其中的两个值。
我认为这可能与index
循环中的foreach
有关,因为每个循环都会增加,并且数组大小会减少,因为要删除元素,但我不确定。
为什么这不起作用,我该如何解决?
答案 0 :(得分:2)
Functional version with jQuery
var theArrays = [[126, 131, 138, 139, 140,143],[126, 201]],result = theArrays[0];
$.each(theArrays, function(index, currentArray) {
result = $.grep(result, function(currentElement) {
return currentArray.indexOf(currentElement) !== -1;
});
});
console.log(result);
简单的JavaScript版本:
var theArrays = [[126, 131, 138, 139, 140,143],[126, 201]],result = theArrays[0];
for (var i = 1; i < theArrays.length; i += 1) {
for (var j = 0; j < result.length; j += 1) {
if (theArrays[i].indexOf(result[j]) === -1) {
result.splice(j, 1);
j -= 1;
}
}
}
console.log(result);
<强>输出强>
[ 126 ]
如果数组太大,那么最好将它们转换为Objects然后找到交集。因为对象中的项目查找对于较大的数组来说会快得多。
var theObjects = [];
for (var i = 0; i < theArrays.length; i += 1) {
var tempObject = {};
for (var j = 0; j < theArrays[i].length; j += 1) {
tempObject[theArrays[i][j]] = true;
}
theObjects.push(tempObject);
}
var intersection = theObjects[0], result = [];
for (var i = 1; i < theArrays.length; i += 1) {
for (var key in intersection) {
if (theObjects[i].hasOwnProperty(key) === false) {
delete intersection[key];
}
}
}
for (var key in intersection) {
result.push(parseInt(key));
}
console.log(result);
答案 1 :(得分:1)
我希望你的问题源于你从数组中删除项目时修改数组长度的事实。您可以使用函数构造一个包含结果的新数组,而不是直接操作数组。我发现这种方法通常可以帮助减少代码中的错误。从您现有的代码可能是这样的:
function intersect(theArray){
var result = [];
for(var i = 0; i < theArray.length; i++){
var row = [];
$.each(theArray[i], function(index, value){
var nextArray = (i+1 < theArray.length
?theArray[i+1]:theArray[0]);
if($.inArray(value, nextArray) != -1) {
row.push(theArray[i][index]);
}
});
result.push(row);
}
return result;
}
Example Here。虽然这确实产生了一个包含交叉点的多维数组 - 你可能只想要它的第一个元素。
答案 2 :(得分:1)
不确切地知道解决方案中的问题所在,但是..
在这里你有一个解决方案
var theArray = [
[126, 131, 138, 139, 140, 143],
[126, 201]
];
//Finds intersection of 2 arrays, returns new array
function intersect_safe(a, b){
var ai=0, bi=0;
var result = [];
while( ai < a.length && bi < b.length ) {
if (a[ai] < b[bi] ){ ai++; }
else if (a[ai] > b[bi] ){ bi++; }
else{ /* they're equal */
result.push(a[ai]);
ai++;
bi++;
}
}
return result;
}
for(var i = 0; i < theArray.length; i++){ // loop through each array of numbers
var nextIndex = (i+1) % theArray.length;
theArray[i] = intersect_safe(theArray[i], theArray[nextIndex]);
}
console.log(theArray);
编辑:如果您想找到没有修改原始数组的交集,只需更改最后一行代码:
var intersection = theArray[0] || [];
for(var i = 0; i < theArray.length; i++){
intersection = intersect_safe(intersection, theArray[i]);
}
console.log(intersection);
输出: [126]
答案 3 :(得分:1)
我稍微更新了你的方法:
for(var i = 0; i < theArray.length; i++) // loop through each array of numbers
{
$.each(theArray[i], function(index, value) // loop through all of the numbers in this array
{
var nextArray = theArray[i + 1];
if(nextArray !== undefined && $.inArray(value, nextArray) == 0)
{
console.log("removing index: " + index + ", value: " + value);
theArray[i].splice(index, 1); // remove the number from the array
}
});
}