Javascript 使用 Lodash 删除数组中的元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28036716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:08:26  来源:igfitidea点击:

Removing elements in an array using Lodash

javascriptlodash

提问by bard

I have this array:

我有这个数组:

var fruits = ['Apple', 'Banana', 'Orange', 'Celery'];

And I use Lodash's removelike so:

remove像这样使用Lodash :

_.remove(fruits, function (fruit) {
  return fruit === 'Apple' || 'Banana' || 'Orange';
})

The result is ['Apple', 'Banana', 'Orange', 'Celery'], while I expected it to be ['Apple', 'Banana', 'Orange']. Why is this so?

结果是['Apple', 'Banana', 'Orange', 'Celery'],而我预期它是['Apple', 'Banana', 'Orange']。为什么会这样?

回答by Amadan

Because when fruitis "Celery", you are testing:

因为 when fruitis "Celery",您正在测试:

"Celery" === 'Apple' || 'Banana' || 'Orange'

which evaluates to

其评估为

false || true || true

which is true.

这是true.

You can't use that syntax. Either do it the long way around:

您不能使用该语法。要么做很长的路:

_.remove(fruits, function (fruit) {
  return fruit === 'Apple' || fruit === 'Banana' || fruit === 'Orange'
});

or test for array membership:

或测试数组成员资格:

_.remove(fruits, function (fruit) {
  return _.indexOf(['Apple', 'Banana', 'Orange'], fruit) !== -1
});

This is not limited to JavaScript, and is in fact a common mistake (e.g. this question)

这不仅限于 JavaScript,而且实际上是一个常见的错误(例如这个问题

回答by gustavogelf

You can use the method _.pullfrom lodash 2.0 and up

您可以使用_.pulllodash 2.0 及更高版本的方法

var fruits = ['Apple', 'Banana', 'Orange', 'Celery'];

_.pull(fruits, 'Apple', 'Banana', 'Orange'); // ['Celery']

document.write(fruits);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.6.1/lodash.js"></script>

回答by Mouscellaneous

If you want to remove a set of items from another set, there are set operations intended specifically for that. Lodash has https://lodash.com/docs/4.17.2#differencewhich takes two array parameters A and B and will return another array which contains all of the elements of A which are not in B.

如果您想从另一个集合中删除一组项目,则有专门用于该集合的操作。Lodash 有https://lodash.com/docs/4.17.2#difference,它接受两个数组参数 A 和 B,并将返回另一个数组,其中包含 A 中不在 B 中的所有元素。

In your case, you could write

在你的情况下,你可以写

const fruits = ['Apple', 'Banana', 'Orange', 'Celery'];
const filteredFruits = _.difference(fruits, ['Apple', 'Banana', 'Orange']);

which will result in ['Celery'].

这将导致['Celery'].

回答by ncksllvn

The problem isn't with Lo-Dash; your problem is with your conditional within your callback function. This:

问题不在于 Lo-Dash;您的问题在于您的回调函数中的条件。这个:

return fruit === 'Apple' || 'Banana' || 'Orange';

Is notcorrect. You need to actually compare fruitwith each string:

正确的。您需要实际fruit与每个字符串进行比较:

return fruit === 'Apple' || fruit === 'Banana' || fruit === 'Orange';

Or, you can use another Lo-Dash function to make it a little more compact:

或者,您可以使用另一个 Lo-Dash 函数使其更紧凑:

_.remove(fruits, function (fruit) {
  return _.contains(['Apple', 'Banana', 'Orange'], fruit);
})

Note:In the latest versions of Lo-Dash the _.containsfunction is deprecated. Please use _.includes

注意:在 Lo-Dash 的最新版本中,该_.contains功能已被弃用。请用_.includes

回答by Sampson

Use an array of values that you'd like to compare against, and check for a returned index greater than -1. This indicates the value evaluated was found in the collection.

使用您想要比较的值数组,并检查返回的索引是否大于 -1。这表明在集合中找到了评估的值。

_.remove( fruits, function ( fruit ) {
  return _.indexOf( [ "Apple", "Banana", "Orange" ], fruit ) >= 0;
});

Alternatively you could use lo-dash's _.containsmethodto get a boolean response.

或者,您可以使用lo-dash 的_.contains方法来获得布尔响应。

The problem with the approach you took was that you weren't comparing fruitagainst each one of those strings; instead, the only comparison taking place was fruitagainst "Apple", after that you were coercing strings all on their own.

您采用的方法的问题在于您没有与fruit这些字符串中的每一个进行比较;相反,唯一发生的比较是fruit针对"Apple",之后你就自己强制字符串了。

Non-empty strings coerce to true(!!"Banana"), and as such are truthy. Therefore, the following condition will always short-circuit at "Banana" (unless fruitstrictly equals "Apple"), returning true:

非空字符串强制为true( !!"Banana"),因此为。因此,以下条件将始终在“香蕉”处短路(除非fruit严格等于"Apple"),返回true

return fruit === "Apple" || 'Banana' || "Orange";