How to filter keys of an object with lodash?

前端 未结 5 1139
野趣味
野趣味 2020-11-28 23:51

I have an object with some keys, and I want to only keep some of the keys with their value?

I tried with filter:

5条回答
  •  情深已故
    2020-11-29 00:24

    Lodash has a _.pickBy function which does exactly what you're looking for.

    var thing = {
      "a": 123,
      "b": 456,
      "abc": 6789
    };
    
    var result = _.pickBy(thing, function(value, key) {
      return _.startsWith(key, "a");
    });
    
    console.log(result.abc) // 6789
    console.log(result.b)   // undefined

提交回复
热议问题