从数组中的对象获取特定值

时间:2022-03-21 12:20:41

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]

我想要得到这样的数组:(2)►[Mike,Ellie]

3 个解决方案

#1


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.

一种方法是使用Array.prototype.filter和Array.prototype.map。但是,首选的方法是使用Array.prototype.reduce,因为它会更有效。

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);

#2


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.

filter不返回回调返回的值,只检查它是否真实,并使用它来决定是否返回数组元素。

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

你可以分两步完成。首先使用过滤器来获取团队===“red”的元素,然后使用map来提取用户名:

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.

您也可以使用reduce一步完成,我将把它留给读者练习。

#3


1  

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

正如@Barmar所建议的,这是一个使用reduce的版本:

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

  return people;
}, []);

#1


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.

一种方法是使用Array.prototype.filter和Array.prototype.map。但是,首选的方法是使用Array.prototype.reduce,因为它会更有效。

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);

#2


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.

filter不返回回调返回的值,只检查它是否真实,并使用它来决定是否返回数组元素。

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

你可以分两步完成。首先使用过滤器来获取团队===“red”的元素,然后使用map来提取用户名:

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.

您也可以使用reduce一步完成,我将把它留给读者练习。

#3


1  

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

正如@Barmar所建议的,这是一个使用reduce的版本:

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

  return people;
}, []);