0

Given this object:

const userData = {
  avatar: undefined,
  name: "Raul",
  username: "raulito",
  celebrity: true
}

I need to implement a method that receives a list of keys (i.e., ["avatar", "name", "username"]) and get the corresponding values of the userData object, ignoring undefined values.

How can I do this using modern javascript syntax?

function denormalizeUserData(userData, ...fields) {
  const denormalized = {};

  // For each key (field), get its value from userData, ignoring if undefined
  
  return denormalized;
}

So, if I do:

 denormalizeUserData(userData, "avatar", "name");

The method must return me:

{
  name: "Raul,
}

Ignoring avatar, as it is undefined.

This is my attempt. I need modern syntax.

const userData = {
  avatar: undefined,
  name: "Raul",
  username: "raulito",
  celebrity: true
}

function denormalizeUserData(userData, ...fields) {
  const denormalized = {};

  fields.forEach((key) => {
    const value = userData[key];

    if(typeof value !== "undefined") {
      denormalized[key] = value;
    } 
  })

  return denormalized;
}

console.log(denormalizeUserData(userData, "celebrity", "name", "avatar"))

1
  • 1
    I'm not sure what the issue is. Your code works, and is readable. Commented Sep 28, 2021 at 23:27

3 Answers 3

1
function denormalizeUserData(userData, ...fields) {
  const denormalized = {};

  for (const field of fields) {
    if (userData[field] !== undefined) denormalized[field] = userData[field];
  }
  
  return denormalized;
}

Edit: in case someone says a code-only answer is blah blah blah

This is simple enough to be a simple code block.

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

2 Comments

This could fail if userData[field] is 0
@Raul Good catch
0

You can get the object property/value pairs with Object.entries, then use Array.filter to filter out the pairs whose property name is not included in fields and whose value is undefined, then use Object.fromEntries to convert it back to an object.

const userData = {
  avatar: undefined,
  name: "Raul",
  username: "raulito",
  celebrity: true
}

function denormalizeUserData(userData, ...fields) {
  return Object.fromEntries(Object.entries(userData).filter(e => fields.includes(e[0]) && e[1] != undefined))
}

console.log(denormalizeUserData(userData, "avatar", "name"))

Comments

0

Grab the Object.entries and build a new object if there is a value, and if the args array includes the key.

const userData = {
  avatar: undefined,
  name: "Raul",
  username: "raulito",
  celebrity: true
};

function denormalizeUserData(obj, ...args) {
  const out = {};
  for (let [key, value] of Object.entries(obj)) {
    if (value && args.includes(key)) {
      out[key] = value;
    };
  }
  return out;
}

console.log(denormalizeUserData(userData));
console.log(denormalizeUserData(userData, 'avatar', 'name'));
console.log(denormalizeUserData(userData, 'avatar', 'name', 'celebrity'));

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.