1

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;
    }
hussain
  • 4,747
  • 12
  • 51
  • 120

5 Answers5

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.

MrBoJangles
  • 11,501
  • 16
  • 60
  • 78
  • ok make sense so how would i check array like that in this use case ? – hussain Jul 09 '19 at 16:15
  • My answer is based on the title of this question. It sounds like you don't need `_isEmpty` for your solution as it stands at the moment. I would recommend updating the title of the question. Or, if possible, have some way to populate `validateResponse` with something that `_isEmpty` regards as empty. – MrBoJangles Jul 09 '19 at 16:27
  • Perhaps it would help to do some detection for an empty object, and there is a question about that here: https://stackoverflow.com/questions/679915/how-do-i-test-for-an-empty-javascript-object – MrBoJangles Jul 09 '19 at 16:28
0

You'll have to compact out the internals or check one level deeper:

if (!validateResponse.filter(r => !_.isEmpty(r)).length){
  throw invalidPriceError;
}
tadman
  • 194,930
  • 21
  • 217
  • 240
0

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.com/ajax/libs/lodash.js/4.17.11/lodash.core.js"></script>
danh
  • 55,236
  • 10
  • 89
  • 124
0

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.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
Ori Drori
  • 145,770
  • 24
  • 170
  • 162
0

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