-1

Is there a way in underscore to filter properties in an object if the values are numbers? I've seen this question, but it deals with booleans.

I want to filter object properties with values greater than one.

From:

[{
  "Tom"  : 10,
  "Dick" : 5,
  "Harry": 0,
  "date" : "02/23/2010
}]

To:

[{
  "Tom"  : 10,
  "Dick" : 5,
  "date" : "02/23/2010
}]

I've looked at the underscore documentation and thought _.pick or _.omit would do the trick, but they only focus on keys.

I've also tried _.filter with _.values as the argument.

Community
  • 1
  • 1
  • 1
    How is the last one processed as a number? – epascarello Mar 30 '16 at 22:55
  • What are you expecting to happen for `date`? – Dominic K Mar 30 '16 at 22:55
  • 1
    *"I've seen this question, but it deals with booleans."* And you can't imagine how it could be changed to handle numbers? – Felix Kling Mar 30 '16 at 22:56
  • @epascarello Not sure what you're asking. –  Mar 30 '16 at 23:01
  • The last entry "date" is not a number, it is a string. So how are you saying it is a number greater than 1? – epascarello Mar 30 '16 at 23:02
  • I'd like the date to remain. I'll just write a conditional to ignore strings. –  Mar 30 '16 at 23:03
  • 1
    Why doesn't the answer to the other question solve your problem? The function you give to `_.pick()` can do anything it wants with the value, including checking whether it's a number and greater than 1. You don't have to use the same function in that question, just use the same general structure. – Barmar Mar 30 '16 at 23:05

1 Answers1

0

You can just remove properties that don't satisfy a certain condition.

for (var key in obj) {
    if (obj[key] < 1) {
        delete obj[key];
    }
}
Evelyn Kokemoor
  • 326
  • 1
  • 9