I have an array of objects like this - say testArray.
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.
.everyand stop negating the length.somereturns true if any items in the array match the condition. Your condition is "is the array empty". soresultwill be true, if any of the items have an empty array. You want the opposite of that, so, negateresult, or use.every