235

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?

Artyom Ionash
  • 405
  • 7
  • 17
Freewind
  • 193,756
  • 157
  • 432
  • 708

8 Answers8

370

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>
Fuji
  • 28,214
  • 2
  • 27
  • 29
serg10
  • 31,923
  • 16
  • 73
  • 94
  • 11
    It seems this is broken in lodash version 4, and the new _.pickBy predicate is only invoked with value not key. Boo :( ...I suppose you can chain _.pick and _.pickBy (actually no you can't get the same functionality) – SDK Jan 15 '16 at 13:03
  • 3
    This no longer works as of the latest Lodash, just a FYI – Eudis Duran May 09 '16 at 18:20
  • 8
    @EudisDuran you have to use `.pickBy` – apfelbox Aug 09 '16 at 11:28
  • seems to have been fixed with the latest version –  Apr 05 '17 at 09:13
48

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>
Artyom Ionash
  • 405
  • 7
  • 17
24

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 }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>
<strong>Open your javascript console to see the output.</strong>
PaulMest
  • 12,925
  • 7
  • 53
  • 50
9

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);
Nicolas Hevia
  • 15,143
  • 4
  • 22
  • 31
5

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}
thomax
  • 9,213
  • 3
  • 49
  • 68
2

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 }
Abilogos
  • 4,777
  • 2
  • 19
  • 39
Daniel Garmoshka
  • 5,849
  • 39
  • 40
1

const data = {
  aaa: 111,
  abb: 222,
  bbb: 333
};
const result = Object.keys(data).filter((val) => val.includes("a"));
console.log(result);
  • This is a non-lodash way of filtering keys of an object, the original problem states a way to filter keys of an object using lodash. – Shahid Manzoor Bhat Mar 30 '21 at 12:42
  • @ShahidManzoorBhat the OP mentioned he's OK with non-lodash solutions, but this solution will return an array of keys, and not an object with just the wanted keys. – Narxx Nov 16 '21 at 14:24
0

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
Narxx
  • 7,929
  • 5
  • 26
  • 34