最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - underscore how to use omit - Stack Overflow

programmeradmin3浏览0评论

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

/

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]}

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

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]}
Share Improve this question asked Sep 25, 2013 at 10:43 bsrbsr 58.7k88 gold badges217 silver badges321 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 14

_omit calls _contains which includes this line of code:

return value === target;

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

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

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

var out = _.omit(test, ['1','2'])
发布评论

评论列表(0)

  1. 暂无评论