在 JavaScript 中过滤数组

Filtering Arrays in Javascript

本文关键字:数组 过滤 JavaScript      更新时间:2023-09-26

假设我有以下两个数组:

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#spliceArray#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