简体   繁体   English

如何在 javascript 中检查对象的属性数组是否为空

[英]How to check if array of object's properties are empty in javascript

I'm trying to make a validation in js to check if array of objects' properties are empty strings or not, to clarify more, i have an array that has objects inside, and i want to check for each object if it has an empty property (""), here is the code i've written but i'm not sure this is the correct way我正在尝试在 js 中进行验证以检查对象的属性数组是否为空字符串,为了进一步澄清,我有一个内部包含对象的数组,我想检查每个 object 是否为空属性(“”),这是我写的代码,但我不确定这是正确的方法

const items = [
  { name: "something", quantity: "25", unit: "d" },
  { name: "something", quantity: "25", unit: "d" },
  { name: "something", quantity: "25", unit: "d" },
];

const validation = items.map((item) => {
  return Boolean(item.name && item.quantity && item.unit);
});

But it is just giving me an array like this:但它只是给了我一个这样的数组:

[true, true, true]

It is like i want it to only give me the value true if all of the properties are not empty如果所有属性都不为空,就像我希望它只给我值 true

Thanks谢谢

You could take Array#every你可以拿Array#every

The every() method tests whether all elements in the array pass the test implemented by the provided function. every()方法测试数组中的所有元素是否通过提供的 function 实现的测试。 It returns a Boolean value.它返回一个 Boolean 值。

and get true if all values are not falsy.如果所有值都不是假的,则为true

 const items = [ { name: "something", quantity: "25", unit: "d" }, { name: "something", quantity: "25", unit: "d" }, { name: "something", quantity: "25", unit: "d" }, ]; const validation = items.every(item => item.name && item.quantity && item.unit); console.log(validation);

Refer below code validation will give true if array doesn't contain empty object else it will give false .如果数组不包含空 object ,请参阅下面的代码验证将给出true ,否则它将给出false

you shouldn't compare each property of object, instead compare object length each time您不应该比较 object 的每个属性,而是每次比较 object 长度

 const items = [ { name: "something", quantity: "25", unit: "d" }, { name: "something", quantity: "25", unit: "d" }, { name: "something", quantity: "25", unit: "d" }, ]; let validation=true; for(let i=0;i<items.lenght;++i){ if(Object.keys(items[i]).length===0) { validation =false; break; } } console.log(validation);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 javascript 中检查和添加属性到数组 object - how to check and add properties to array object in javascript 如何检查 object 中的数组是否为空 JavaScript - How to check if array inside object is empty JavaScript 如何检查Javascript对象的值是否为数组而不为空 - How to check if Javascript object value is array and not empty 检查数组是否为空 javascript - check if array it´s empty javascript 如何检查数组中是否存在多个 object 属性并使用 Javascript 获取值是否为真 - How to check if exist multiple object properties inside an array and get a value if it's true using Javascript 如何检查是否存在具有相同密钥的 object 并使用数组(javascript)中的相同密钥和不同属性进行更新? - How can I check if there's an object with a same key and update with a same key and different properties in array(javascript)? 如何在Javascript中检查对象数组中的空对象属性 - How to Check for an empty object property in an array of objects in Javascript 如何检查对象是否为空? [javascript] - How to check if an object is empty? [javascript] 如何检查对象数组是否为空? - How to check if array of object is empty? Javascript 检查 object 数组中的所有值是否为空 - Javascript check if all values in an object array are empty
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM