1
let data = [
{
    id: 900,
    name: 'bar',
    slug: 'julep-mask',
    status: 'publish',
    categories: [{
        id: 43,
        name: 'Beauty',
        slug: 'beauty',
        categories: [{
            id: 777,
            name: 'Beauty',
            slug: 'asdfghjkl',
            categories: [{
                id: 999999,
                name: 'Antony',
                slug: 'Moss',
            }]
        }]
    }]
},
{
    id: 700,
    name: 'foo',
    slug: 'julep-mask',
    status: 'publish',
    categories: [{
        id: 43,
        name: 'Beauty',
        slug: 'beauty',
        categories: [{
            id: 777,
            name: 'Beauty',
            slug: 'asdfghjkl',
            categories: []
        }]
    }]
},
{
    id: 999,
    name: 'foobar',
    slug: 'julep-mask',
    status: 'publish',
    categories: [{
        id: 43,
        name: 'Beauty',
        slug: 'beauty',
        categories: [{
            id: 777,
            name: 'Beauty',
            slug: 'asdfghjkl',
            categories: [{
                id: 12345678,
                name: 'Jo',
                slug: 'Bloggs',
            }]
        }]
    }]
},
{
    id: 11111,
    name: 'fib',
    slug: 'julep-mask',
    status: 'publish',
    categories: [{
        id: 43,
        name: 'Beauty',
        slug: 'beauty',
        categories: [{
            id: 777,
            name: 'Beauty',
            slug: 'asdfghjkl',
            categories: []
        }]
    }]
}];

Given this example data array. I need to remove any object where the final categories array is empty. So I would expect to have an array which looks like this [{ id: 999 }, { id: 900 }] as a result.

I've tried the following, but this only get's me to the second categories key and wouldn't work for continuously nested data.

    const populatedCategories = categories.map(category => {
    const categories = category.categories.filter(cat => cat. categories);
    if (categories.length) {
        return {
            ...category,
            categories
        };
    }
    return null;
}
).filter(p => p);

enter code here
1
  • 2
    Each of these categories arrays has only one item. Can it have more than 1 category at any level? If yes, what should happen when one of the path has empty at the end the the other one doesn't Commented Feb 7, 2023 at 15:09

1 Answer 1

3

const data = [{"id":900,"name":"bar","slug":"julep-mask","status":"publish","categories":[{"id":43,"name":"Beauty","slug":"beauty","categories":[{"id":777,"name":"Beauty","slug":"asdfghjkl","categories":[{"id":999999,"name":"Antony","slug":"Moss"}]}]}]},{"id":700,"name":"foo","slug":"julep-mask","status":"publish","categories":[{"id":43,"name":"Beauty","slug":"beauty","categories":[{"id":777,"name":"Beauty","slug":"asdfghjkl","categories":[]}]}]},{"id":999,"name":"foobar","slug":"julep-mask","status":"publish","categories":[{"id":43,"name":"Beauty","slug":"beauty","categories":[{"id":777,"name":"Beauty","slug":"asdfghjkl","categories":[{"id":12345678,"name":"Jo","slug":"Bloggs"}]}]}]},{"id":11111,"name":"fib","slug":"julep-mask","status":"publish","categories":[{"id":43,"name":"Beauty","slug":"beauty","categories":[{"id":777,"name":"Beauty","slug":"asdfghjkl","categories":[]}]}]}]

const f = o=>!o.categories || o.categories.length && o.categories.every(f)

console.log(data.filter(f))

console.log(data.filter(f).map(({id})=>({id})))

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

2 Comments

Woah! Can you explain how this is working please as I'm inexperienced.
Firstly, I wasn't sure whether you wanted a result with just the ids, or the whole objects, so I did it both ways. The filter function takes each object in the array and invokes the function f with the object. If the result of that function is true, the object remains, otherwise it is excluded. The f function immediately returns true if there is no categories property. But if there is, it checks both that it has items inside it, and that each item also recursively either has no categories property, or a non-empty categories property. Recursive code can be a bit tricky to digest

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.