6

Suppose I have a list of objects with many keys and I want to keep only certain keys from them. This is how I am doing it.

The Problem with other good solutions on SO are that if a key is not present in the keys to keep it still adds a key, value where the value is undefined.

let data = [{
   'a': 1,
   'b': 2,
   'c': 3
 }, 
 {
   'a': 1,
   'c': 3,
   'd': 4
 }]

const keys_to_keep = ['a', 'b']

data = data.map((obj) => {
  Object.keys(obj).forEach(function(key) {
    if(!keys_to_keep.includes(key))
      delete obj[key]
  });
  return obj;
})

Output :

[ { a: 1, b: 2 }, { a: 1} ]

Is there a better way to get this done. Any help is appreciated.

6
  • 4
    What problems are you having with the way you're doing it now? Commented Sep 9, 2020 at 23:52
  • What is the expected output? Commented Sep 9, 2020 at 23:55
  • stackoverflow.com/questions/54907549/… Commented Sep 9, 2020 at 23:57
  • 1
    If you happen to already be using lodash, see stackoverflow.com/questions/30726830/… Commented Sep 9, 2020 at 23:59
  • @epascarello You can run the code on the SO link you shared with my data. It gives me [ { a: 1, b: 2 }, { a: 1, b: undefined } ], whereas I wanted [ { a: 1, b: 2 }, { a: 1} ] . That is exactly what I have explained. Commented Oct 11, 2020 at 4:15

2 Answers 2

10

A couple of improvements.

  1. You're using .map() which creates a new array, but then you're just assigning it to the old variable. So, you apparently don't need to create a new array at all, you can just iterate the one you have and modify it.

  2. Put the properties you want to keep in a Set instead of an Array for potentially faster lookup.

  3. for/of loops are generally favored over .forEach() loops because of better flow control options (break, continue, return, etc...) and more opportunities for compiler optimization.

let kv = [{
   'a': 1,
   'b': 2,
   'c': 3
 }, 
 {
   'a': 1,
   'b': 2,
   'c': 3,
   'd': 4
 }]

const l = new Set(['a', 'b']);

for (let obj of kv) {
    for (let prop of Object.keys(obj)) {
       if (!l.has(prop)) {
           delete obj[prop];
       }
    }
}

console.log(kv);

Sign up to request clarification or add additional context in comments.

Comments

3

You can use Object.fromEntries, after map and filter to keep only the relevant keys:

let data = [{'a': 1,'b': 2,'c': 3},{'a': 1,'c': 3,'d': 4}]
const keys_to_keep = ['a', 'b']

var result = data.map(obj =>
    Object.fromEntries(keys_to_keep.map(key => 
        obj.hasOwnProperty(key) && [key, obj[key]]
    ).filter(Boolean))
);

console.log(result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.