1

I want to remove unnecessary key values pairs in a user object based on an array, essentially a whitelist. I have looked at reduce(), filter() and map() but can't quite get the implementation to work.

Here is my code currently

const user = {
   "email": "[email protected]"
   "name": "Bob"
   "surname": "Smith"
};

const userFields = [`email`, `name`]; // array of whitelist

Before I iterate through the object properties, I'd like to reduce Object to have fields only in the array whitelist, so our man Bob Smith would end up like this

const user = {
  "email": "[email protected]" 
  "name": "Bob"
};

Thanks!

1
  • 1
    Question: Do you want to modify the object in place, or create a new object? Commented Jan 14, 2021 at 16:36

3 Answers 3

1

Use reduce on userFields and build

const user = {
  email: "[email protected]",
  name: "Bob",
  surname: "Smith",
};

const userFields = [`email`, `name`];

const newUser = userFields.reduce(
  (acc, key) => Object.assign(acc, { [key]: user[key] }),
  {}
);
console.log(newUser)

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

Comments

0

For me, the simplest thing to do would be to loop through the passlist and build an object:

const result = {};
for (const name of userFields) {
    result[name] = user[name];
}

Live Example:

const user = {
   "email": "[email protected]",
   "name": "Bob",
   "surname": "Smith",
};

const userFields = [`email`, `name`];

const result = {};
for (const name of userFields) {
    result[name] = user[name];
}

console.log(result);

You could use map and Object.fromEntries, but I don't think it buys you anything:

const result = Object.fromEntries(
    userFields.map(name => [name, user[name]])
);

Live Example:

const user = {
   "email": "[email protected]",
   "name": "Bob",
   "surname": "Smith",
};

const userFields = [`email`, `name`];

const result = Object.fromEntries(
    userFields.map(name => [name, user[name]])
);

console.log(result);


FWIW, for me reduce is the wrong tool for this (or indeed, almost anything else outside of Functional Programmning with predefined, reusable reducer functions), not least because the accumulator in a reduce solution would never change (it's always the same object). reduce where a simple loop suffices is just overcomplicated and easy to get wrong. (An opinion which isn't solely mine, btw.)

1 Comment

@VLAZ - LOL, quite. What was I thinking?!
0

You could use Object.fromEntries() method. Traverse the userFields array using Array.prototype.map() and make a list of key-value pair array. At last, from that list transform it into the required object by using Object.entries() method.

const user = {
  email: '[email protected]',
  name: 'Bob',
  surname: 'Smith',
};

const userFields = [`email`, `name`];
const ret = Object.fromEntries(userFields.map((x) => [x, user[x]]));
console.log(ret);

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.