javascript 下划线如何使用省略

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19002745/
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-10-27 14:02:23  来源:igfitidea点击:

underscore how to use omit

javascriptunderscore.js

提问by bsr

How does underscore's omit work. I was expecting to remove properties with key 1 and 2 below. but it is not.

下划线的省略如何工作。我期待删除下面的键 1 和 2 的属性。但事实并非如此。

http://jsfiddle.net/FMaDq/1/

http://jsfiddle.net/FMaDq/1/

var test = {
    1: [],
    2: [],
    3: [],
    4: []
}

var out = _.omit(test, [1,2])
var out2 = _.omit(test, 1,2)
console.log(out)
console.log(out2)

Object {1: Array[0], 2: Array[0], 3: Array[0], 4: Array[0]}
Object {1: Array[0], 2: Array[0], 3: Array[0], 4: Array[0]}

回答by Quentin

_omitcalls _containswhich includes this line of code:

_omit_contains包括这行代码的调用:

return value === target;

The keys will be strings, so you need to pass strings in to compare to (since "1" === 1is false).

键将是字符串,因此您需要将字符串传入以进行比较(因为"1" === 1为 false)。

_.omit(test, "1", "2")

回答by bsr

I guess key needs to be string. This worked. http://jsfiddle.net/FMaDq/2/

我猜键需要是字符串。这奏效了。 http://jsfiddle.net/FMaDq/2/

var out = _.omit(test, ['1','2'])