[英]Check for property values in array of objects
我有这个数组:
var data= [{IsNormal:"true", Name:"Mike"},
{IsNormal:"true", Name:"Tom"},
{IsNormal:"false", Name:"Clause"},
{IsNormal:"true", Name:"Timm"},
{IsNormal:"true", Name:"Marta"},
{IsNormal:"true", Name:"Dora"}];
我需要编写函数来检查数组中至少一个对象的IsNormal属性是否等于false,如果函数必须返回false,否则必须返回true。
这是我的实现:
function chekStatus(data) {
_.each(inspections, function (value, key) {
if (!value.IsNormal)
return false;
return true;
});
}
但是我想使用数组原型javascript功能编写更精美的东西。
使用Array.some
function chekStatus(data) {
return data.some(function (item) { return !item.IsNormal; })
}
注意, IsNormal
值应为boolean
,而不是string
var data= [{IsNormal:true, Name:"Mike"},
{IsNormal: true, Name:"Tom"},
{IsNormal: false, Name:"Clause"},
{IsNormal: true, Name:"Timm"},
{IsNormal: true, Name:"Marta"},
{IsNormal: true, Name:"Dora"}];
您可以使用Array.some()
data.some(obj => !obj.IsNormal)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.