1

I am trying to return an array from a json object. Each object has some data and another nested array/object

like this:

{
    "data": [
        {
            "name": "name",
            "id": "id",
            "amount": 4000,
            "items": [
                {
                    "number": 12,
                    "isAvailable": true,
                },
                {
                    "number": 15,
                    "isAvailable": true,
                },
                {
                    "number": 16,
                    "isAvailable": true,
                },
                {
                    "number": 17,
                    "isAvailable": true,
                }
            ]
        },
        {
            "name": "name2",
            "id": "id2",
            "amount": 3000,
            "items": [
                {
                    "number": 12,
                    "isAvailable": true,
                },
                {
                    "number": 15,
                    "isAvailable": true,
                },
                {
                    "number": 16,
                    "isAvailable": false,
                },
                {
                    "number": 17,
                    "isAvailable": true,
                }
            ]
        },
        {
            "name": "name4",
            "id": "id4",
            "amount": 1200,
            "items": [
                {
                    "number": 12,
                    "isAvailable": true,
                },
                {
                    "number": 15,
                    "isAvailable": true,
                },
                {
                    "number": 16,
                    "isAvailable": true,
                },
                {
                    "number": 17,
                    "isAvailable": true,
                }
            ]
        }
    ]
 }

I want to check all objects and see if in any of these objects has a item that has "isAvailable": false If an item is not available I need to remove the parent object from the array (so not the item array but the full object above that.

Then I would like to return the array where this object has been removed.

I got this to work by using ES6 findIndex

array.findIndex(object => object.items.some(item => !item.isAvailable));

Which will return the index of the parent array that has it, which I can use to splice the array. But this only works on a single object. If I have another object it will still return the first index.

How can I check if multiple objects exist based on the criteria and remove them from an array?

3
  • Your Object isn't of valid syntax, there are quotes behind some numbers: "number": 12" Commented Sep 4, 2018 at 10:28
  • Use .filter instead of .findIndex Commented Sep 4, 2018 at 10:33
  • @LucaKiebel Oops, that was a user error. This is dummy data :) thanks for noticing Commented Sep 4, 2018 at 11:07

1 Answer 1

1

You might use .filter instead of .findIndex and then reassign the array to the new filtered array:

const removed = data.filter(object => object.items.some(item => !item.isAvailable));
data = data.filter(item => !removed.includes(item));

If you need to mutate and not just reassign, you might splice while filtering:

const removed = data.filter(object => {
  const toRemove = object.items.some(item => !item.isAvailable);
  if (!toRemove) return false;
  // Have to use `indexOf` rather than the second filter argument
  // because the index of the object might change due to an earlier splice:
  data.splice(data.indexOf(object), 1);
  return true;
});
Sign up to request clarification or add additional context in comments.

1 Comment

Ah! Reassigning is what I missed. I tried to do filter instead of findIndex but it did not work. Now I know why! Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.