How to filter keys of an object with lodash?

前端 未结 5 1143
野趣味
野趣味 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条回答
  •  萌比男神i
    2020-11-29 00:45

    Native ES2019 one-liner

    const data = {
      aaa: 111,
      abb: 222,
      bbb: 333
    };
    
    const filteredByKey = Object.fromEntries(Object.entries(data).filter(([key, value]) => key.startsWith("a")))
    
    console.log(filteredByKey);

提交回复
热议问题