2
Is there any way to make one filter in an array and retrieve what it does and what is not part of the condition?
For example the following object:
{
"data": [
{ "item": 1 },
{ "item": 2 },
{ "item": 3 },
{ "item": 4 },
{ "item": 5 },
{ "item": 6 }
]
}
Return a result of type:
{
"conditionTrue": [
{ "item": 1 },
{ "item": 2 },
{ "item": 3 }
],
"conditionFalse": [
{ "item": 4 },
{ "item": 5 },
{ "item": 6 }
]
}
Filtering in the following way I can obtain the elements of the true case and those of the negative case:
a = {"data":[{"item":1},{"item":2},{"item":3},{"item":4},{"item":5},{"item":6}]};
a.data.filter(el => [1,2,3].includes(el.item));
// [{"item":1},{"item":2},{"item":3}]
a.data.filter(el => ![1,2,3].includes(el.item));
// [{"item":4},{"item":5},{"item":6}]
However, I wanted to make only one filter to obtain the condition if true and if negative.
I tried to do something like the following, but it didn’t work.
a = {"data":[{"item":1},{"item":2},{"item":3},{"item":4},{"item":5},{"item":6}]};
a.data.filter(el => {
return {
conditionTrue: [1, 2, 3].includes(el.item),
conditionFalse: ![1, 2, 3].includes(el.item)
}
});