简体   繁体   English

如何使用 javascript 或 lodash 返回指示 object 数组元素为空的 boolean 值

[英]How to return a boolean value indicating an element of an array of object is empty using javascript or lodash

I have an array of object as below.我有一个 object 数组,如下所示。

[{key:1, value:'value1'},{key:2, value:'value2'},{key:3, value:''}]

I want to check by looping entire dynamic array and return a boolean value which indicates any of the value is empty using plain javascript and lodash.我想通过循环整个动态数组来检查并返回一个 boolean 值,该值指示任何值都是空的,使用普通的 javascript 和 lodash。 If in the array of object all value element is not empty it should return false and if any of the value is empty it should return true .如果在 object 的数组中所有value元素都不为空,则应返回false ,如果任何value空,则应返回true Can anyone please help me to solve the same.谁能帮我解决同样的问题。

Use Array.some() to iterate the array, and for every object check if the value is an empty string.使用Array.some()迭代数组,并为每个 object 检查值是否为空字符串。 The method will stop and return true , as soon as such value is found.一旦找到这样的值,该方法就会停止并返回true If none found, it will return false .如果没有找到,它将返回false

 const arr = [{key:1, value:'value1'},{key:2, value:'value2'},{key:3, value:''}] const result = arr.some(o => o.value === '') console.log(result)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM