I have some object like this
const array =
{
"entities": [
{
"annexes": [
{
"buildingUniqueIds": []
},
{
"buildingUniqueIds": []
},
{
"buildingUniqueIds": []
},
],
"buildingUniqueIds": []
}
]
}
I need to check if any of those array is empty to return true, is this possible to do in one iteration, here is what I have tried for now, but some does not check array length it always return true even if array is empty :(
get haveBuildingUniqueIds(): boolean {
const condition1 = this.array.entities.some(x => x.annexes.some(y => y.buildingUniqueIds.some));
const condition2 = this.array.entities.some(x => x.buildingUniqueIds.some);
return condition1 && condition2;
}
CodePudding user response:
some
is the right method to use, but it's a method, not a flag, so your innermost .some(y => y.buildingUniqueIds.some)
will always return true
, because a method is a truthy value.
Instead, check length
:
const hasEmpties = array.entities.some(({annexes, buildingUniqueIds}) =>
buildingUniqueIds.length === 0 ||
annexes.some(({buildingUniqueIds}) => buildingUniqueIds.length === 0)
);
Live Example:
function check(label, array) {
const hasEmpties = array.entities.some(({annexes, buildingUniqueIds}) =>
buildingUniqueIds.length === 0 ||
annexes.some(({buildingUniqueIds}) => buildingUniqueIds.length === 0)
);
console.log(label, "=>", hasEmpties);
}
check("all empty", {
"entities": [
{
"annexes": [
{
"buildingUniqueIds": []
},
{
"buildingUniqueIds": []
},
{
"buildingUniqueIds": []
},
],
"buildingUniqueIds": []
}
]
});
check("none empty", {
"entities": [
{
"annexes": [
{
"buildingUniqueIds": [{}]
},
{
"buildingUniqueIds": [{}]
},
{
"buildingUniqueIds": [{}]
},
],
"buildingUniqueIds": [{}]
}
]
});
check("entities.buildingUniqueIds empty", {
"entities": [
{
"annexes": [
{
"buildingUniqueIds": [{}]
},
{
"buildingUniqueIds": [{}]
},
{
"buildingUniqueIds": [{}]
},
],
"buildingUniqueIds": []
}
]
});
check("entities.annexes.buildingUniqueIds[1] empty", {
"entities": [
{
"annexes": [
{
"buildingUniqueIds": [{}]
},
{
"buildingUniqueIds": []
},
{
"buildingUniqueIds": [{}]
},
],
"buildingUniqueIds": [{}]
}
]
});
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
Side note: If you like, you can use !buildingUniqueIds.length
rather than buildingUniqueIds.length === 0
.
Or without destructuring if you prefer:
const hasEmpties = array.entities.some(entity =>
entity.buildingUniqueIds.length === 0 ||
entity.annexes.some(annex => annex.buildingUniqueIds.length === 0)
);
Live Example:
function check(label, array) {
const hasEmpties = array.entities.some(entity =>
entity.buildingUniqueIds.length === 0 ||
entity.annexes.some(annex => annex.buildingUniqueIds.length === 0)
);
console.log(label, "=>", hasEmpties);
}
check("all empty", {
"entities": [
{
"annexes": [
{
"buildingUniqueIds": []
},
{
"buildingUniqueIds": []
},
{
"buildingUniqueIds": []
},
],
"buildingUniqueIds": []
}
]
});
check("none empty", {
"entities": [
{
"annexes": [
{
"buildingUniqueIds": [{}]
},
{
"buildingUniqueIds": [{}]
},
{
"buildingUniqueIds": [{}]
},
],
"buildingUniqueIds": [{}]
}
]
});
check("entities.buildingUniqueIds empty", {
"entities": [
{
"annexes": [
{
"buildingUniqueIds": [{}]
},
{
"buildingUniqueIds": [{}]
},
{
"buildingUniqueIds": [{}]
},
],
"buildingUniqueIds": []
}
]
});
check("entities.annexes.buildingUniqueIds[1] empty", {
"entities": [
{
"annexes": [
{
"buildingUniqueIds": [{}]
},
{
"buildingUniqueIds": []
},
{
"buildingUniqueIds": [{}]
},
],
"buildingUniqueIds": [{}]
}
]
});
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>