1

Considering the following array which includes several objects within it, I tried to get specific values in one single array.To put it delicately,I wanted to pull out the name of people who were in the "red" team.

// 
    const array = [
      {
        username: "Mike",
        team: "red",
        score: 20,
     items: ["bat", "book", "gloves"]
  },
  {
    username: "Moe",
    team: "blue",
    score: 30,
    items: ["cellphone", "backpack", "cards"]
  },
  {
    username: "Ellie",
    team: "red",
    score: 15,
    items: ["ball", "canteen", "crayon"]
  },
  {
    username: "little_joe",
    team: "green",
    score: 1,
    items: ["book", "pencil"]
  },

];

//using filter method :

const filterArray=array.filter((num)=>{ if (num.team==="red" ){ return num.username}});
console.log(filterArray);

//result : an array including two objects full of unwanted values.

What should I do to get one single array with only desired value ( not two big objects) ?

I'm trying to get an array like this : (2)►[Mike,Ellie]

3 Answers 3

1

One way to do this would be to use Array.prototype.filter and Array.prototype.map. However, the preferred way would be to use Array.prototype.reduce as it will be much more efficient.

Like this:

const array = [{
    username: "Mike",
    team: "red",
    score: 20,
    items: ["bat", "book", "gloves"]
  },
  {
    username: "Moe",
    team: "blue",
    score: 30,
    items: ["cellphone", "backpack", "cards"]
  },
  {
    username: "Ellie",
    team: "red",
    score: 15,
    items: ["ball", "canteen", "crayon"]
  },
  {
    username: "little_joe",
    team: "green",
    score: 1,
    items: ["book", "pencil"]
  },

];

const newArray = array.reduce((n, val) => {
  if(val.team == "red") n.push(val.username);
  return n;
}, []);

console.log(newArray);

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

2 Comments

it's fine, but why this gimmick usage of ternary operator val.team == "red" ? n.push(val.username) : null; instead of straightforward if(val.team == "red") n.push(val.username); ?
@jedzej you're right. I shouldn't be using ternary for ternary's sake. Answer updated. Thanks.
1

filter doesn't return the values that were returned by the callback, it just checks if it's truthy, and uses that to decide whether to return the array element.

You can do it in two steps. First use filter to get just the elements with team === "red", then use map to extract username:

const filterArray = array.filter(num => num.team === "red").map(num => num.username);

You can also do it in one step using reduce, I'll leave that as an exercise for the reader.

Comments

1

As suggested by @Barmar, here's a version using reduce:

const redTeamPeople = array.reduce((people, { team, username }) => {
  if (team === "red") {
    people.push(username);
  }

  return people;
}, []);

1 Comment

This is the preferred method. More concise and much faster given a very large array with a lot of data.

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.