简体   繁体   English

从对象数组中提取具有另一个属性等于给定值的项目的属性值数组

[英]From array of objects extract array of property values for items having another property equal to given value

An array is given below.下面给出了一个数组。 I would like to get the username for every single member of this array whose team is red!我想获取该数组中团队为红色的每个成员的用户名!

const array = [
  {
    username: "john",
    team: "red",
    score: 5,
    items: ["ball", "book", "pen"]
  },
  {
    username: "becky",
    team: "blue",
    score: 10,
    items: ["tape", "backpack", "pen"]
  },
  {
    username: "susy",
    team: "red",
    score: 55,
    items: ["ball", "eraser", "pen"]
  },
  {
    username: "tyson",
    team: "green",
    score: 1,
    items: ["book", "pen"]
  },

];

I've tried this code:我试过这段代码:

const colorTeam = array.filter(teams=>teams.team === 'red');
console.log('teamColor:', username);

It didn't work!它没有用!

As opposed to filter() + map() (two passes over the source array), one may use Array.prototype.reduce() to achieve that in a single pass (which may give certain performance gain should input array be large enough, or such filtering performed often enough):filter() + map() (源数组上的两次传递)相反,可以使用Array.prototype.reduce()来实现单次传递(如果输入数组足够大,这可能会带来一定的性能增益,或经常执行此类过滤):

 const array = [{username:"john",team:"red",score:5,items:["ball","book","pen"]},{username:"becky",team:"blue",score:10,items:["tape","backpack","pen"]},{username:"susy",team:"red",score:55,items:["ball","eraser","pen"]},{username:"tyson",team:"green",score:1,items:["book","pen"]}], redTeamNames = array.reduce((acc, {username, team}) => (team == 'red' && acc.push(username), acc), []) console.log(redTeamNames)
 .as-console-wrapper{min-height:100%;}

If this is what you wanted?如果这是你想要的? First we filter objects with team === red , then we map the array to contain username property only.首先我们用team === red过滤对象,然后我们 map 数组只包含username属性。

 const array = [ { username: "john", team: "red", score: 5, items: ["ball", "book", "pen"] }, { username: "becky", team: "blue", score: 10, items: ["tape", "backpack", "pen"] }, { username: "susy", team: "red", score: 55, items: ["ball", "eraser", "pen"] }, { username: "tyson", team: "green", score: 1, items: ["book", "pen"] }, ]; const colorTeam = array.filter(teams=>teams.team === 'red').map(user=>user.username); console.log('teamColor:', colorTeam);

user Array.filter followed by Array.map :用户Array.filter后跟Array.map

 const array=[{username:"john",team:"red",score:5,items:["ball","book","pen"]},{username:"becky",team:"blue",score:10,items:["tape","backpack","pen"]},{username:"susy",team:"red",score:55,items:["ball","eraser","pen"]},{username:"tyson",team:"green",score:1,items:["book","pen"]}]; let result = array.filter(({team}) => team === "red").map(({username}) => username) console.log(result)

In one line:在一行中:

let newArray = array.filter((el) => el.team==="red" ? el : "").map((el) => el.username);

First, filter objects with team property named "red", then use map method to get username properties only.首先,过滤具有名为“red”的团队属性的对象,然后使用 map 方法仅获取用户名属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从对象数组中提取属性值作为数组 - From an array of objects, extract value of a property as array JavaScript:从对象数组中的对象数组中提取属性值作为数组 - JavaScript: from an array of objects of an array of objects, extract value of a property as array 如果另一个属性具有给定值,则在对象数组中总结某些属性值 - In array of objects sum up certain property values if another property has given value 如何按属性值对对象数组进行排序并在该值相等时求和? - How to sort an array of objects by property values and sum when this value is equal? 从动态对象数组中,如何将 mutltple 属性的值提取为数组 - From an array of dynamic objects, how to extract value of a mutltple property as array 从对象数组中,仅提取属性的特定值作为数组 - From an array of objects, extract only specific value of a property as array 如何将具有相同属性名称的对象数组转换为从给定数组中的值连接值的对象? JS - How to transform an array of objects with the same property name into the object in which value is concatenated from values in a given array? JS "根据属性值 lodash 将对象数组中的属性合并到另一个对象中" - Merge property from an array of objects into another based on property value lodash 计算具有相同属性值的数组对象 - Calculation on array objects having same property values 从嵌套的对象和数组数组中提取属性值 - Extract property values from nested array of objects and arrays
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM