简体   繁体   English

如何在我的 Angular 应用程序中的数组中从 Object 中获取价值

[英]How To Get Value From An Object Inside An Array In My Angular Application

So I have this array of object coming from an API in my Angular Application所以我在我的 Angular 应用程序中有来自 API 的 object 数组

"register":[
{
"club": 8,
"players": 100,
"officials": 10
},
{
"male": 7,
"female": 2,
"other": 1
},
{
"Brazil": 5,
"France": 1,
"Italy": 2,
"England": 2
}
]

My aim is to display an item in the third object on the console.我的目标是在控制台的第三个 object 中显示一个项目。 So I tried looping through the array of object this way:所以我尝试以这种方式循环遍历 object 的数组:

 let country =  data['register'].map(data => data.England)
  console.log(country) // Output: [undefined, undefined, 2]

I guess the undefined are coming from the first two objects.我猜未定义来自前两个对象。

How do I loop through this object to get only the figure for England in the third Object, without the first two undefined showing?如何循环遍历此 object 以仅在第三个 Object 中获得英格兰的数字,而前两个未定义显示?

Note :注意

data is coming from the API I subscribed to in the .ts file.数据来自我在.ts文件中订阅的 API。 It contains all the results from the API.它包含来自 API 的所有结果。

You can .filter() out all objects which don't have a England key (and keep all those which do) and then .map() your array of objects like so:你可以.filter()出所有没有England键的对象(并保留所有那些),然后.map()你的对象数组,如下所示:

 const data = {register:[{club:8,players:100,officials:10},{male:7,female:2,other:1},{Brazil:5,France:1,Italy:2,England:2}]}; let country = data.register.filter(obj => 'England' in obj) // keep all objects in your array with the England property.map(obj => obj.England) // map to the england property console.log(country) // Output: [2]

If England can never have a value of 0 , you can .filter() after mapping by using Boolean() like so:如果England的值永远不能为0 ,则可以在映射后使用Boolean()进行.filter() ,如下所示:

 const data = {register:[{club:8,players:100,officials:10},{male:7,female:2,other:1},{Brazil:5,France:1,Italy:2,England:2}]}; const country = data.register.map(({England}) => England).filter(Boolean); console.log(country) // Output: [2]


EDIT: As per your question in the comments, if you wish to get the number value of each property into an array, you can use .flatMap with Object.values() :编辑:根据您在评论中的问题,如果您希望将每个属性的数值放入数组中,您可以使用.flatMapObject.values()

 const data = {register:[{club:8,players:100,officials:10},{male:7,female:2,other:1},{Brazil:5,France:1,Italy:2,England:2}]}; let country = data.register.filter(obj => 'England' in obj) // keep all objects in your array with the England property.flatMap(Object.values) // flatMap the array returned by Object.values console.log(country) // Output: [5, 1, 2, 2]

Or with lodash (more browser compatible):或者使用 lodash(兼容更多浏览器):

 const data = {register:[{club:8,players:100,officials:10},{male:7,female:2,other:1},{Brazil:5,France:1,Italy:2,England:2}]}; const englandObjects = _.filter(data.register, obj => 'England' in obj) const numberVals = _.flatMap(englandObjects, Object.values); console.log(numberVals);
 <script src="https://cdn.jsdelivr.net/lodash/4.16.4/lodash.min.js"></script>

you can use _.compact(array) built-in function from Lodash which removes all the falsy values like false, null, 0, "", undefined and NaN.您可以使用_.compact(array)内置 function 删除所有虚假值,如 false、null、0、“”、未定义和 NaN。

let country = _.compact(data['register'].map(data => data.England));
console.log(country) // Output: [2]

Use case of map is totally different than what you are trying to achieve. map 的用例与您想要实现的完全不同。 Map iterates over your collections and for each item under collections, produce new result and will be returned that. Map 迭代您的 collections 并为 collections 下的每个项目生成新结果并将返回该结果。 So for first two object it not finding so Undefined .因此,对于前两个 object 它没有找到Undefined You have 3 object, you got array of 3 with first two being undefined Use map when you want to process collections and wants new collection with your desired result.您有 3 个 object,您有 3 个数组,前两个未定义当您想要处理 collections 并想要具有所需结果的新集合时使用 map。

Here you can use find:在这里您可以使用查找:

const foundItem = data.register.find(item => item.England)
console.log(foundItem.England); // you can access all attributes here
console.log( [foundItem.England] ); // as array

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM