javascript - 如何从对象数组中检查对象中值的长度
问题描述
使用过滤器()
使用运动员数据数组和 .filter():
仅返回运动员收入超过 7 个字符的运动员对象
- 将返回的数据存储在新
results
变量中 * - 笔记:
不要删除运动员数据变量
不要更改任何运动员数据内容
const athleteData = [ { athlete: 'Lionel Messi', team: 'Barcelona', income: 40000000 }, { athlete: 'Cristiano Ronaldo', team: 'Juventus', income: 30000000 }, { athlete: 'Neymar', team: 'Paris Saint-Germain', income: 36800000 }, { athlete: 'Eden Hazard', team: 'Chelsea', income: 10400000 }, { athlete: 'Mohamed Salah', team: 'Liverpool', income: 4680000 }, { athlete: 'Kylian Mbappé', team: 'Paris Saint-Germain: An American Musical', income: 17500000 }, { athlete: 'Luka Modrić', team: 'Real Madrid', income: 9360000 }, { athlete: 'Harry Kane', team: 'Tottenham Hotspurs', income: 17600000 }, { athlete: 'Kevin De Bruyne', team: 'Manchester City', income: 5980000 }, { athlete: 'Paul Pogba', team: 'Manchester United', income: 15080000 } ]; const results = 'Replace this message with your code!'; console.log(results);
**我想做的方式是**:
常量结果=运动员数据.filter(排序=>排序.收入.长度> 7);
解决方案
我刚刚filter
按照你们的规定发表了声明:
const athleteData = [{
athlete: 'Lionel Messi',
team: 'Barcelona',
income: 40000000
},
{
athlete: 'Cristiano Ronaldo',
team: 'Juventus',
income: 30000000
},
{
athlete: 'Neymar',
team: 'Paris Saint-Germain',
income: 36800000
},
{
athlete: 'Eden Hazard',
team: 'Chelsea',
income: 10400000
},
{
athlete: 'Mohamed Salah',
team: 'Liverpool',
income: 4680000
},
{
athlete: 'Kylian Mbappé',
team: 'Paris Saint-Germain: An American Musical',
income: 17500000
},
{
athlete: 'Luka Modrić',
team: 'Real Madrid',
income: 9360000
},
{
athlete: 'Harry Kane',
team: 'Tottenham Hotspurs',
income: 17600000
},
{
athlete: 'Kevin De Bruyne',
team: 'Manchester City',
income: 5980000
},
{
athlete: 'Paul Pogba',
team: 'Manchester United',
income: 15080000
}
];
const results = athleteData.filter(({ income }) => income.toString().length > 7);
console.log(results);
这个过滤器athleteData
像这样:
首先,我们解构 ( { income }
) 以获取income
每个对象的属性。
接下来,我们将其转换为字符串 ( .toString()
),以便我们可以检查length
每个对象的 。
然后我们只是为了console.log(results)
让你可以看到数据。
推荐阅读
- python - Find Alphabet and Color it to Red with OpenCV Python
- c# - 关于使用 UI 按钮更改对象不透明度的问题
- java - 双向链表问题。一次迭代会是什么样子?
- android - 为什么 LiveData 比 MutableLiveData 更好?
- winapi - ShellExecute bat 文件提升(FMX,Win32)
- gradle - Spring Boot Gradle 脚本 - 获取 mainClassName
- javascript - 使用 Mozilla 导航器从剪贴板粘贴内容
- python-3.x - 元组列表理解
- java - 有人可以帮我用java打印这个模式或者帮我处理逻辑吗
- node.js - UnhandledPromiseRejectionWarning 即使代码在 async/await 中有 try/catch