【发布时间】:2021-06-10 21:34:59
【问题描述】:
const arr = [{id: 1, name: '123', price: 11, discount: 0, quantity: 1, total: 1}, {id: 2, name: '123', price: 11, discount: 0, quantity: 1, total: 1}];
const productsValidation = (data) => data.every(
({ id, name, price, discount, quantity, total }) =>
id && name && price && discount && quantity && total
);
console.log(productsValidation(arr));
我想检查数组中的对象是否包含我期望的值,但由于某种原因 each 返回 false。为什么?
【问题讨论】:
-
因为
discount的值为0,这是虚假的。如果您只是想检查是否存在一个或多个键,则使用Object.keys获取所有键并将其与您希望对象具有的键数组相交,或使用<key> in <object>。目前尚不清楚您要在代码中检查什么。 -
@Terry 哦,真的,谢谢你的提示
标签: javascript