javascript - How to check if object is empty using lodash _isEmpty? - Stack Overflow

i have map function that is returning empty object for the array now if i check array _isEmpty this con

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
Add a ment  | 

5 Answers 5

Reset to default 1

As 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;
    }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745356474a4624149.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信