最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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;
    }
发布评论

评论列表(0)

  1. 暂无评论