javascript - 如果任何键为假,则过滤对象数组
问题描述
我怎样才能过滤这样的数组
[
{ foo: NaN, test: 'String' },
{ foo: 2, test: '' },
{ foo: 3, test: 'Something' },
]
并返回一个没有对象的新数组,其中键的值是虚假的?在这种情况下,应该只返回带有第三个对象的数组。
[
{ foo: 3, test: 'Something' },
]
解决方案
只需过滤并检查对象的所有值。
var array = [{ foo: NaN, test: 'String' }, { foo: 2, test: '' }, { foo: 3, test: 'Something' }],
result = array.filter(o => Object.values(o).every(Boolean));
console.log(result);
推荐阅读
- materialize - 物化芯片和 addChip() 方法
- c# - 将从服务器返回的内容映射到 Angular 服务中的打字稿类型
- javascript - Cytoscape.js 网格布局不使用网格位置
- php - 在 Guzzle 中解决多个异步承诺时,我只能从第一个承诺中获得结果
- ios - 卡片样式 CollectionViewCell
- html - 如何在包含滚动条的父 div 中垂直居中子 div?
- java - Java Spring:请求的资源上不存在“Access-Control-Allow-Origin”标头
- javascript - create-react-app:无法安装站点:未检测到匹配的服务人员
- perl - 获取一年中的一周(一周的第一天是星期日)无法返回第 53 周
- swift - 如何从可解码的枚举中获取原始值?迅速