我想了解从另一个数组的所有元素中过滤一个数组的最佳方法。我尝试了过滤功能,但它不来我如何给它的值,我想删除。喜欢的东西:

var array = [1,2,3,4];
var anotherOne = [2,4];
var filteredArray = array.filter(myCallback);
// filteredArray should now be [1,3]


function myCallBack(){
    return element ! filteredArray; 
    //which clearly can't work since we don't have the reference <,< 
}

如果过滤器函数没有用处,您将如何实现它? 编辑:我检查了可能的重复问题,这可能对那些容易理解javascript的人有用。如果答案勾选“好”,事情就简单多了。


当前回答

您可以使用过滤器,然后为过滤器函数使用过滤数组的约简,当它找到匹配时检查并返回true,然后在返回(!)时反转。filter函数对数组中的每个元素调用一次。在你的文章中,你没有对函数中的任何元素进行比较。

Var a1 = [1,2,3,4], A2 = [2,3]; Var filter = a1.filter(函数(x) { 返回! a2。Reduce(函数(y, z) { 返回x == y || x == z || y == true; }) }); document . write(过滤);

其他回答

如果你需要比较一个对象数组,这在所有情况下都适用:

let arr = [{ id: 1, title: "title1" },{ id: 2, title: "title2" }]
let brr = [{ id: 2, title: "title2" },{ id: 3, title: "title3" }]

const res = arr.filter(f => brr.some(item => item.id === f.id));
console.log(res);

使用对象筛选结果

[{id:1},{id:2},{id:3},{id:4}].filter(v=>!([{id:2},{id:4}].some(e=>e.id === v.id)))

Jack Giffin的解决方案很好,但不适用于大于2^32的数组。下面是基于Jack的解决方案来过滤数组的重构快速版本,但它适用于64位数组。

const Math_clz32 = Math.clz32 || ((log, LN2) => x => 31 - log(x >>> 0) / LN2 | 0)(Math.log, Math.LN2);

const filterArrayByAnotherArray = (searchArray, filterArray) => {

    searchArray.sort((a,b) => a > b);
    filterArray.sort((a,b) => a > b);

    let searchArrayLen = searchArray.length, filterArrayLen = filterArray.length;
    let progressiveLinearComplexity = ((searchArrayLen<<1) + filterArrayLen)>>>0
    let binarySearchComplexity = (searchArrayLen * (32-Math_clz32(filterArrayLen-1)))>>>0;

    let i = 0;

    if (progressiveLinearComplexity < binarySearchComplexity) {
      return searchArray.filter(currentValue => {
        while (filterArray[i] < currentValue) i=i+1|0;
        return filterArray[i] !== currentValue;
      });
    }
    else return searchArray.filter(e => binarySearch(filterArray, e) === null);
}

const binarySearch = (sortedArray, elToFind) => {
  let lowIndex = 0;
  let highIndex = sortedArray.length - 1;
  while (lowIndex <= highIndex) {
    let midIndex = Math.floor((lowIndex + highIndex) / 2);
    if (sortedArray[midIndex] == elToFind) return midIndex; 
    else if (sortedArray[midIndex] < elToFind) lowIndex = midIndex + 1;
    else highIndex = midIndex - 1;
  } return null;
}

如果你想过滤一个具有一些匹配属性的不同结构的数组,你应该这样做。

let filteredArray = [];

array1.map(array1Item => {
array2.map(array2Item => {
  if (array1.property1 === array2.property2) {
    filteredArray.push(array1Item);
  }
});

这会让你的生活变得轻松!

这完全取决于数组的类型。

对于简单的数组,比如字符串数组。你可以像@Redu和@Hugolpz指出的那样使用下面的代码

 var arr1 = [1,2,3,4],
        arr2 = [2,4],
        res = arr1.filter(item => !arr2.includes(item));
    console.log(res);

然后,对于更复杂的数组过滤器,比如从另一个对象数组中过滤一个对象数组,您可以使用下面的代码

function filterArray(arr1, arr2) {
  return arr1.filter(item1 => !arr2.some(item2 => item1.id === item2.id));
}

OR

function filterArray(arr1, arr2) {
  const set = new Set(arr2.map(item => item.id));
  return arr1.reduce((filtered, item) => {
    if (!set.has(item.id)) {
      filtered.push(item);
    }
    return filtered;
  }, []);
}

这两种方法都是有效的,即使使用大型数组也能很好地工作。但是,如果您有大量数据,则使用性能更好的数据结构(如Map或HashSet)可能会更优。

Map

function filterArray(arr1, arr2) {
  const map = new Map(arr2.map(item => [item.id, item]));
  return arr1.filter(item => !map.has(item.id));
}

Hashset

function filterArray(arr1, arr2) {
  const set = new Set(arr2.map(item => item.id));
  return arr1.filter(item => !set.has(item.id));
}