admin管理员组文章数量:1422029
i have map function that is returning empty object for the array now if i check array _isEmpty
this condition should satisfy but its not getting into if statement. Any idea what is implemented wrong or better approach ?
main.js
const validateResponse = _.map(drugs ,validateValues);
now validateResponse returns [{}] and it should satisfy condition
if (_.isEmpty(validateResponse)) {
throw invalidPriceError;
}
i have map function that is returning empty object for the array now if i check array _isEmpty
this condition should satisfy but its not getting into if statement. Any idea what is implemented wrong or better approach ?
main.js
const validateResponse = _.map(drugs ,validateValues);
now validateResponse returns [{}] and it should satisfy condition
if (_.isEmpty(validateResponse)) {
throw invalidPriceError;
}
Share
Improve this question
asked Jul 9, 2019 at 16:03
hussainhussain
7,14121 gold badges87 silver badges165 bronze badges
3
-
1
So is the question how to detect
[{}]
? – danh Commented Jul 9, 2019 at 16:05 - @danh thats correct – hussain Commented Jul 9, 2019 at 16:07
-
_.some(validateResponse, _.isEmpty)
seems close, but not sure how you'd want to handle an array of length != 1 – danh Commented Jul 9, 2019 at 16:15
5 Answers
Reset to default 1As per the lodash documentation here:
Array-like values such as arguments objects, arrays, buffers, strings, or jQuery-like collections are considered empty if they have a length of 0. Similarly, maps and sets are considered empty if they have a size of 0.
[{}].length
happens to be 1. A cabbage-in-a-box, if you will. An array with one empty object. Hence, isEmpty evaluates to false. [].length
, on the other hand, equals 0.
You'll have to pact out the internals or check one level deeper:
if (!validateResponse.filter(r => !_.isEmpty(r)).length){
throw invalidPriceError;
}
There might be a handful of other cases you want to cover, like empty array, or an array of two empty objects, etc. Variations on the following should do what you need...
let array = [{}];
// contains any empty object
console.log(_.some(array, _.isEmpty))
// contains only empty objects
console.log(_.every(array, _.isEmpty))
// contains exactly one empty object
console.log(_.every(array, _.isEmpty) && array.length == 1)
<script src="https://cdnjs.cloudflare./ajax/libs/lodash.js/4.17.11/lodash.core.js"></script>
If you just want to check if there is a single array with a single empty object [{}]
you can use _.isEqual
:
const hasEmpty = arr => _.isEqual(arr, [{}])
console.log(hasEmpty([])) // false
console.log(hasEmpty([{}])) // true
console.log(hasEmpty([{}, {}])) // false
console.log(hasEmpty([{ a: 1 }])) // false
<script src="https://cdnjs.cloudflare./ajax/libs/lodash.js/4.17.11/lodash.js"></script>
Since the array isn't actually empty, but what you're truly checking for is "does the array have exactly one value, and is that single value an empty object?", you could just do this check:
if (validateResponse.length === 1 && _.isEmpty(validateResponse[0])) {
throw invalidPriceError;
}
本文标签: javascriptHow to check if object is empty using lodash isEmptyStack Overflow
版权声明:本文标题:javascript - How to check if object is empty using lodash _isEmpty? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745356473a2655084.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论