在 JavaScript 中过滤数组
Filtering Arrays in Javascript
假设我有以下两个数组:
var arrOne = [1, 4, 7];
var arrTwo = [1, 2, 3, 4, 5];
var arrThree = [];
我想对arrTwo
进行迭代,如果它包含一个也在arrOne
中的元素,请将其从arrTwo
中删除,然后将其插入arrThree
中。所以看看上面的数组,之后数组的状态应该是这样的:
var arrOne = [1, 4, 7];
var arrTwo = [2, 3, 5];
var arrThree = [1, 4];
谁能指出我正确的方向和最好的方法?如果提供了代码,将非常感谢分步解释,以便我能够理解正在发生的事情。
一个简单的 for 循环,与 indexOf 和拼接匹配匹配。
var arrOne = [1, 4, 7];
var arrTwo = [1, 2, 3, 4, 5];
var arrThree = [];
for (var i = 0; i < arrTwo.length; i++) {
if (arrOne.indexOf(arrTwo[i]) >= 0) {
arrThree.push(arrTwo[i]);
arrTwo.splice(i, 1);
i--;
}
}
console.log(arrOne, arrTwo, arrThree)
Array.IndexOf
Array.splice
查看下划线库。 arrOne 中所有也在 arrTwo 中的元素都称为 _.intersection()
。
使用简单的 while 循环和 Array#splice
和Array#unshift
方法。
var arrOne = [1, 4, 7];
var arrTwo = [1, 2, 3, 4, 5];
var arrThree = [];
// get length of array
var l = arrTwo.length;
// iterate over array from the end
while (l--) {
// check value present in arrOne
if (arrOne.indexOf(arrTwo[l]) > -1)
// if present then remove and insert it
// at the beginning of arrThree
arrThree.unshift(arrTwo.splice(l, 1)[0])
}
console.log(arrTwo, arrThree);
嗨,
您可以使用过滤器功能来过滤数组。尝试使用以下代码。
var arrOne = [1, 4, 7];
var arrTwo = [2, 3, 5, 1];
var arrThree = [];
function checkValue(a) {
return !arrOne.indexOf(a);
}
function checkValue2(a) {
return arrThree.indexOf(a);
}
function myFunction() {
arrThree = arrTwo.filter(checkValue);
document.getElementById("demo").innerHTML = arrThree ;
arrTwo = arrTwo.filter(checkValue2);
document.getElementById("demo1").innerHTML = arrTwo;
}
var arrOne = [1, 4, 7];
var arrTwo = [1, 2, 3, 4, 5];
var arrThree = diff(arrOne,arrTwo);//passes the two arrays
console.log(arrThree);
function diff(one, two){
one.forEach(function(e1){ //iterate through the first array
two.forEach(function(e2){//iterate through second
if(e1 == e2) //checking if elements are equal
two.pop(e2);//removing from second array
});
});
return two; //returning new array
}
您可以使用下划线 js 进行简单的数组操作,差分操作将_.difference([1, 2, 3, 4, 5], [5, 2, 10]);
。
var array1 = [1, 2, 3, 4, 5, 6],
var array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var common = $.grep(array1, function(element) {
return $.inArray(element, array2) !== -1;
});
console.log(common); // returns [1, 2, 3, 4, 5, 6];
array2 = array2.filter(function(obj) {
return array1.indexOf(obj) == -1;
});
// returns [7,8,9];
由于数组是排序的,你可以并行读取它们:O(n(而不是O(n2(。不要使用库来解决这么简单的问题,这是矫枉过正:-(
var i = 0, j = 0;
var a = [1, 4, 7];
var b = [1, 2, 3, 4, 5];
var c = [];
while (i < a.length && j < b.length) {
if (a[i] < b[j]) i++;
else if (a[i] > b[j]) j++;
else c.push(b.splice(j, 1)[0]);
}
console.log("a " + toString(a));
console.log("b " + toString(b));
console.log("c " + toString(c));
function toString (v) {
return "[ " + v.join(" ") + " ]";
}
跟踪:
#0 init
a = [ 1 4 7 ]
i
b = [ 1 2 3 4 5 ]
j
c = []
#1 a[i] = b[j] => move b[j] to c
a = [ 1 4 7 ]
i
b = [ 2 3 4 5 ]
j
c = [ 1 ]
#2 a[i] < b[j] => increment i
a = [ 1 4 7 ]
i
b = [ 2 3 4 5 ]
j
c = [ 1 ]
#3 a[i] > b[j] => increment j
a = [ 1 4 7 ]
i
b = [ 2 3 4 5 ]
j
c = [ 1 ]
#4 a[i] > b[j] => increment j
a = [ 1 4 7 ]
i
b = [ 2 3 4 5 ]
j
c = [ 1 ]
#5 a[i] = b[j] => move b[j] to c
a = [ 1 4 7 ]
i
b = [ 2 3 5 ]
j
c = [ 1 4 ]
#6 a[i] < b[j] => increment i
a = [ 1 4 7 ]
i
b = [ 2 3 5 ]
j
c = [ 1 4 ]
#7 a[i] > b[j] => increment j
a = [ 1 4 7 ]
i
b = [ 2 3 5 ]
j
c = [ 1 4 ]
#8 j = length of b => done
相关文章:
- 使用一个键的值数组过滤多个javascript对象
- 根据另一个包含角度 js 中对象的数组过滤包含对象的数组
- Javascript:按字符串数组过滤对象数组
- 在 javascript 中使用数组过滤 for AngularJS
- 挖空和选择器的数组过滤问题
- Lodash 按数组的属性数组过滤
- 如何用另一个字符串数组过滤ng重复中的字符串数组
- 如何根据javascript数组过滤rss数据
- 基于NodeJS中的其他数组过滤/搜索对象的JavaScript数组
- 使用不带嵌套循环的数组过滤对象数组js
- 使用数组过滤数组Javascript
- 用一维数组过滤多维数组
- AngularJS:如何给值数组过滤
- Angularjs,逐个数组过滤
- 用另一个对象数组过滤Javascript对象数组
- 创建搜索栏以将数组过滤到表中
- 通过id数组过滤角度数据
- coffeescript -数组过滤不工作
- Backbone.js:如何通过模型ID数组过滤对象集合
- 使用基于嵌套值的数组过滤对象数组