0

Im working on a challenge: Given an array of objects, write a function admin that returns the name and birthdate of all users that are marked as "admin".

const users = [
  {
    name: 'Homer', 
    role: 'clerk', 
    dob: '12/02/1988',
    admin: false 
  }, 
  {
    name: 'Lisa', 
    role: 'staff', 
    dob: '01/30/1965',
    admin: false 
  }, 
  {
    name: 'Marge', 
    role: 'associate', 
    dob: '09/10/1980',
    admin: true 
  }
]

function admin(myObj) {
  function groupBy(myObj, prop) {
  return myObj.reduce(function (acc, obj) {
    let key = obj[prop]
    if (!acc[key]) {
      acc[key] = []
    }
    acc[key].push(obj)
    return acc
    }, {});
  }
  let trueAdmin = groupBy(myObj, 'admin');
  console.log(trueAdmin[true].map(({name, dob}) => `Name: ${name}
Dob: ${dob}
`).join(`
`));
}

admin(users);

When I run tests, it comes back as undefined and i do not know why. Im looking for an expected output: admin(users)

// Name: Marge 
// Dob: 09/10/1980

1 Answer 1

1

If you just want to log them to the console you can simply use forEach()

const users = [
  {
    name: 'Homer', 
    role: 'clerk', 
    dob: '12/02/1988',
    admin: false 
  }, 
  {
    name: 'Lisa', 
    role: 'staff', 
    dob: '01/30/1965',
    admin: false 
  }, 
  {
    name: 'Marge', 
    role: 'associate', 
    dob: '09/10/1980',
    admin: true 
  }
]

function admin(myArray) {
  myArray.forEach(user => {
    if (user.admin) console.log(`Name: ${user.name}, DOB: ${user.dob}`);
  });
}

admin(users);


If you want to return the matched users you can use filter() to return an array filtered by a callback, and then map() that to alter the elements of the filtered array so they conform to the expected output.

In the following snippet admin() returns an array of mutated user objects with only name and dob keys.

const users = [
  {
    name: 'Homer', 
    role: 'clerk', 
    dob: '12/02/1988',
    admin: false 
  }, 
  {
    name: 'Lisa', 
    role: 'staff', 
    dob: '01/30/1965',
    admin: false 
  }, 
  {
    name: 'Marge', 
    role: 'associate', 
    dob: '09/10/1980',
    admin: true 
  }
]

function admin(myArray) {
  return myArray.filter(u => u.admin).map(u => ({name: u.name, dob: u.dob}));
}

console.log(admin(users));

filter(u => u.admin) iterates over each object and returns an array containing elements for which user.admin is true.

map(u => ({name: u.name, dob: u.dob})) then iterates over each element of the filtered array and changes each one to be an object containing just name: and dob: of the passed object.

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

1 Comment

Thanks for clearing up the difference between simply printing properties to the screen and getting the array.map technique to actually return the desired properties. I've only been coding a few weeks now and it is cool to see the variety of built in functions that can display the data in different ways. I appreciate your feedback and guidance.

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.