我有一个对象数组,在我想发送请求之前,如果价格为空,我想显示一条消息。所以我用了.every,但它总是返回 false “是否为空”样本数据const services = [{ "id": 12, "price": "10" }, { "id": 12, "price": "" }, { "id": 12, "price": "99" },]const isEmpty = (value) => value === undefined || value === null || value === '';const doEmptyAction = services.every((item) => isEmpty(item.price));console.log(doEmptyAction); // always false
1 回答
繁星淼淼
TA贡献1775条经验 获得超11个赞
every() 不会对没有值的数组元素执行函数。使用一些():
const services = [{
"id": 12,
"price": "10"
},
{
"id": 12,
"price": ""
},
{
"id": 12,
"price": "99"
},
]
const isEmpty = (value) => value === undefined || value === null || value === '';
const doEmptyAction = services.some((item) => isEmpty(item.price));
console.log(doEmptyAction);
添加回答
举报
0/150
提交
取消