How to filter keys of an object with lodash?

JavascriptFilterLodash

Javascript Problem Overview


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

I tried with filter:

const data = {
  aaa: 111,
  abb: 222,
  bbb: 333
};

const result = _.filter(data, (value, key) => key.startsWith("a"));

console.log(result);

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>

But it prints an array:

> [111, 222]

Which is not what I want.

How to do it with lodash? Or something else if lodash is not working?

Javascript Solutions


Solution 1 - Javascript

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

<script src="https://cdn.jsdelivr.net/lodash/4.16.4/lodash.min.js"></script>

Solution 2 - Javascript

Just change filter to omitBy

const data = { aaa: 111, abb: 222, bbb: 333 };
const result = _.omitBy(data, (value, key) => !key.startsWith("a"));
console.log(result);

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>

Solution 3 - Javascript

Here is an example using lodash 4.x:

const data = {
  aaa: 111,
  abb: 222,
  bbb: 333
};

const result = _.pickBy(data, (value, key) => key.startsWith("a"));

console.log(result);
// Object { aaa: 111, abb: 222 }

Solution 4 - Javascript

A non-lodash way to solve this in a fairly readable and efficient manner:

function filterByKeys(obj, keys = []) {
  const filtered = {}
  keys.forEach(key => {
    if (obj.hasOwnProperty(key)) {
      filtered[key] = obj[key]
    }
  })
  return filtered
}

const myObject = {
  a: 1,
  b: 'bananas',
  d: null
}

const result = filterByKeys(myObject, ['a', 'd', 'e'])
console.log(result) // {a: 1, d: null}

Solution 5 - Javascript

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);

Solution 6 - Javascript

const data = {
  aaa: 111,
  abb: 222,
  bbb: 333
};
const result = Object.keys(data).filter((val) => val.includes("a"));
console.log(result);

Solution 7 - Javascript

You can use the _.omit() function.

it accepts the keys to filter as an string array like below:

var object = { a: 1, b: 2, c: 3 }
_.omit(object, ['a', 'c'])
// => { b: 2 }

Solution 8 - Javascript

Pure JS solution:

const data = {
  aaa: 111,
  abb: 222,
  bbb: 333
};

const result = Object.keys(data).filter( key => {
  return key.indexOf('a') == 0
}).map( key => {
  return { key: data[key] }
})

console.log(result)
  1. Filter through the data for keys that starts with the letter 'a'
  2. Map those keys with the proper value, and return as key:value object

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFreewindView Question on Stackoverflow
Solution 1 - Javascriptserg10View Answer on Stackoverflow
Solution 2 - JavascriptKrystian JankowskiView Answer on Stackoverflow
Solution 3 - JavascriptPaulMestView Answer on Stackoverflow
Solution 4 - JavascriptthomaxView Answer on Stackoverflow
Solution 5 - JavascriptNicolas HeviaView Answer on Stackoverflow
Solution 6 - JavascriptAlamkheer Husainul Fareedh MView Answer on Stackoverflow
Solution 7 - JavascriptDaniel GarmoshkaView Answer on Stackoverflow
Solution 8 - JavascriptNarxxView Answer on Stackoverflow