Filtering object by keys in lodash

后端 未结 3 1117
-上瘾入骨i
-上瘾入骨i 2021-02-20 03:42

I wrote the function below to return all keys in an object that match a specific pattern. It seems really round-about because there\'s no filter function in lodash for objects,

3条回答
  •  自闭症患者
    2021-02-20 04:09

    Here's a succinct way to do this with lodash - reduce() and set() are your friends.

    _.reduce(data, (result, value, key) => key.match(pattern) ? 
      _.set(result, key, value) : result, {});
    

提交回复
热议问题