3

I have an array of objects like this - say testArray. enter image description here I need to check that for each object in array, if any of the saveNumbers array is empty, my function should return false.

My function is as follows:

  public checkSaveNumbers(): boolean {
    let result;
    if (this.testArray) {
      result = _.some(this.testArray, member => !member.saveNumbers.length);
    }
    console.log(result);
  }

Here, result is showing true for my above object where we can see that the first element in the array has saveNumbers array empty. I'd expect result to be false in that case. I want to achieve this using Lodash and I'm not sure what is going wrong here.

Update:

As suggested in comments, I changed the code to following:

public checkSaveNumbers(): boolean {
  let result = true;
  if (this.testArray) {
    result = _.every(this.testArray, member => member.saveNumbers.length > 0); //as soon as the condition is false, result should become false otherwise result will remain true.
    console.log(result);
  }
}

However, with this code, even when my saveNumbers array is non-empty for every member in testArray, I get result as false.

8
  • uhm.... .some returns true if one of them are true. In your case, one of them are true, so result will be true... simply negate it, or use .every and stop negating the length Commented Dec 1, 2016 at 19:51
  • if I negate it, it always returns false even if saveNumbers isn't empty. Commented Dec 1, 2016 at 19:55
  • .some returns true if any items in the array match the condition. Your condition is "is the array empty". so result will be true, if any of the items have an empty array. You want the opposite of that, so, negate result, or use .every Commented Dec 1, 2016 at 19:56
  • I don't see why anyone would downvote a question like this! @KevinB I changed my code to result = _.every(this.selectedMembers, member => member.saveNumbers.length); now, result shows false even when saveNumbers contains elements. Commented Dec 1, 2016 at 19:58
  • it's unclear. Your code does exactly what it should do, but you expect a different result... so you shoudl change it to have the expected result instead. I don't see what the problem is. Commented Dec 1, 2016 at 19:59

3 Answers 3

4

If any of the saveNumbers array is empty, my function should return false.

You tried:

_.some(array, member => !member.saveNumbers.length)

But this means "true if at least one member has empty saveNumbers." Which is exactly the opposite of what you want.

So negate the entire expression.

!_.some(array, member => !member.saveNumbers.length)

UPDATE:

If this is also not working for you:

_.every(array, member => member.saveNumbers.length > 0)

then you have a bug somewhere else.

Sign up to request clarification or add additional context in comments.

1 Comment

You're right. Although in logs I see that my array contains the values, it still gives me false with _.every. I think its something to do with my Angular app. Thanks for your help.
2

You can rewrite your function as follow:

function checkSaveNumber(testArray) {
    return !_.some(_.map(testArray, 'saveNumbers'), ['length', 0]);  
}

http://jsbin.com/domitexazo/edit?js,console

Comments

1

another variant

!_.chain(testArray)
    .map('saveNumbers')
    .map(_.isEmpty)
    .some()
    .value()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.