Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

underscore how to use omit

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]}
like image 442
bsr Avatar asked Sep 25 '13 10:09

bsr


People also ask

What does _ omit do?

The _. omit() function is used to return a copy of the object that filtered to omit the blacklisted keys. Parameters: This function accept two parameters as mentioned above and described below: object: This parameter holds the value of an object.

How do you omit in JavaScript?

omit() method is used to return a copy of the object that composed of the own and inherited enumerable property paths of the given object that are not omitted. It is the opposite of the _. pick() method.

What is omit in js?

With the omit() method of Underscore. js, we can return an object's properties by omitting some specified ones. We only need to specify the properties we want to omit.


1 Answers

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

var out = _.omit(test, ['1','2'])
like image 200
bsr Avatar answered Sep 17 '22 13:09

bsr