javascript - 迭代对象数组中存在的对象数组
问题描述
children.BallAdequacy 是一个对象数组。其中 playerRank 是一个对象数组。从每个 playerRank 数组中,我需要在控制台中分别显示 Ball 和 playerHeight 值。我使用了 map 和 filter 方法,但仍然无法为每个对象打印球和 playerHeight。你能告诉我如何解决它。在下面提供我的代码片段和数据
例如每个对象应该打印222--->HEIGHT,ww22w22--->HEIGHT 等
let children = {
BallAdequacy: [{
"flight": "dd",
"specialty": "ff",
"playerRank": [{
"Ball": "222",
"playerHeight": "HEIGHT"
},
{
"Ball": "ddeeeew",
"playerHeight": "NON-HEIGHT"
},
],
"hospitalPrivilege": []
},
{
"flight": "kkk",
"specialty": "ff",
"playerRank": [{
"Ball": "kfkf",
"playerHeight": "HEIGHT"
},
{
"Ball": "All",
"playerHeight": "NON-HEIGHT"
}
],
"hospitalPrivilege": []
}
]
};
children.BallAdequacy.map(status => {
console.log("status.playerRank--->", status.playerRank);
status.playerRank.filter(game => {
//console.log("game.playerHeight--->", game.playerHeight);
if (game.playerHeight === 'HEIGHT') {
console.log("after if --->", game);
console.log("after if --->", game.Ball);
}
// (game.playerHeight === 'HEIGHT')
//console.log("outsidei f--->", game);
});
console.log("after filter status.playerRank--->", status.playerRank);
//BallList = getBalls(status.playerRank);
});
解决方案
遵循和的map()
定义filter()
该
map()
方法使用对调用数组中的每个元素调用提供的函数的结果创建一个新数组。该
filter()
方法创建一个新数组,其中包含通过所提供函数实现的测试的所有元素。
两种方法都返回一个新数组,它必须克隆一个新对象,但您不需要。您所需要的只是console.log
一些东西,并且没有更改数据。你应该forEach
改用。并删除一些不必要的控制台。
let children = {
BallAdequacy: [{
"flight": "dd",
"specialty": "ff",
"playerRank": [{
"Ball": "222",
"playerHeight": "HEIGHT"
},
{
"Ball": "ww22w22",
"playerHeight": "HEIGHT"
},
{
"Ball": "wwwww",
"playerHeight": "NON-HEIGHT"
},
{
"Ball": "ddeeeew",
"playerHeight": "NON-HEIGHT"
},
,
{
"Ball": "All",
"playerHeight": "NON-HEIGHT"
}
],
"hospitalPrivilege": []
},
{
"flight": "kkk",
"specialty": "ff",
"playerRank": [{
"Ball": "kfkf",
"playerHeight": "HEIGHT"
},
{
"Ball": "iioioo",
"playerHeight": "HEIGHT"
},
{
"Ball": "24jk",
"playerHeight": "NON-HEIGHT"
},
{
"Ball": "All",
"playerHeight": "NON-HEIGHT"
}
],
"hospitalPrivilege": []
}
]
};
children.BallAdequacy.forEach(status => {
status.playerRank.forEach(game => {
if (game.playerHeight === 'HEIGHT') {
console.log(`${game.Ball} ---> ${game.playerHeight}`);
}
});
});
推荐阅读
- mysql - 如何查询出现在一个表中的记录,但不在其他两个表中
- javascript - golang javascript对象等效
- python - 如果 name/id 是前几年的条目,如何检查每个时间序列条目?
- python - Python / pynput:如何在变化时获得相对鼠标位置?
- html - 如何在使用表单和脚本发送电子邮件后隐藏确认屏幕
- sql - FROM 子句中的 Delphi 语法错误,但是 - 没有 FROM 子句
- python - 在浏览器的本地主机上显示从服务器发送到客户端的内容
- python - 熊猫在x轴上绘制时间戳 - 刻度单位?
- java - onOptionsItemSelected() 方法不会在 Fragment 上被调用
- python-3.x - 我们如何使用 Python 或 Flask 通过网络浏览器播放 IP 摄像机的实时流(RTSP 链接)?