简体   繁体   English

使用.filter()方法返回多个项目-JavaScript

[英]Return Multiple items with the .filter() method - JavaScript

If I have an array with a set of numbers, and want to remove certain ones with the .filter method, I understand how to do this in when i need to remove a single item (or if i can use a mathematical expression), but can't get it to work with multiple items either purely by their value or position in the array. 如果我有一个带有一组数字的数组,并且想使用.filter方法删除某些数组,那么我了解当我需要删除单个项目(或者如果我可以使用数学表达式)时该如何做,但是不能仅仅通过它们的值或在数组中的位置来使其与多个项目一起使用。

In the following code I would like the new array selection to return the numbers 10, 12, 15 在下面的代码中,我希望新数组选择返回数字10、12、15

Also, I do need to do this with the filter() method only. 另外,我确实只需要使用filter()方法来执行此操作。

JS JS

let random = [4, 10, 12, 15, 30];
let selection = random.filter(function(num){

  return num === [10, 12, 30];

});

You can use includes : 您可以使用includes

 let random = [4, 10, 12, 15, 30, 10, 10, 12, 5]; let selection = random.filter(function(num){ var good = [10, 12, 30] return good.includes(num); }); console.log(selection) 

Of if you prefer the terseness of arrow function: 如果您更喜欢箭头功能的简洁性:

 let random = [4, 10, 12, 15, 30, 10, 10, 12, 5]; let selection = random.filter(num => [10, 12, 30].includes(num)) console.log(selection) 

Forgive me if I do not understand it correctly, but if what you're looking for is to filter an array by the item's index, you can use the second parameter passed to the filter method's callback, which is the index . 如果我不能正确理解它,请原谅我,但是如果您要查找的是根据项目的索引过滤数组,则可以使用传递给filter方法的回调的第二个参数index

 let random = [4, 10, 12, 15, 30]; let selection = random.filter(function(num, index){ return index > 0 && index < 4; }); console.log(selection); 

You do need a condition that will return true or false when using filter, so the simplest way to do this with the filter method would be: 您确实需要一个条件,该条件在使用filter时将返回true或false,因此使用filter方法执行此操作的最简单方法是:

let random = [4, 10, 12, 15, 30];
let selection = random.filter(function(num){

if (num > 4 && num < 16) {return true};

});

The other way to do it would be filtering by position in the array and that would be better achieved with other array methods. 另一种方法是按数组中的位置进行过滤,而使用其他数组方法会更好。

If I understand your question you want a function that takes a an array, checks if the elements are in your larger array and if they are there, removes them. 如果我理解您的问题,则需要一个采用数组的函数,请检查元素是否在较大的数组中,如果它们在其中,则将其删除。

Filter might not be your best choice here because it doesn't alter the original array, it only makes a new array. 过滤器可能不是您的最佳选择,因为它不会更改原始数组,只会生成一个新数组。 Try using splice. 尝试使用接头。

 let random = [4, 10, 12, 15, 30]; function remove(array) { for (var i = 0; i < array.length; i++) { if(random.includes(array[i])){ random.splice(random.indexOf(array[i]), 1) } } } remove([10, 12, 30]); console.log(random); // [4, 15] 

I am assuming you want to remove because if you already know which elements you want why filter them out? 我假设您要删除,因为如果您已经知道要删除哪些元素,为什么要过滤掉它们? why not just pass them into your function as a new array? 为什么不将它们作为新数组传递给函数? filter will create a new array anyways. filter仍然会创建一个新数组。

But if you would like to remove elements from your first array the answer above might help. 但是,如果您想从第一个数组中删除元素,上述答案可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM