Skip to content
Advertisement

Filtering boolean and returning parent title

let chains: {
    keys: {
        "key1": {
            inStock: boolean;
        };
        "key2": {
            inStock: boolean;
        };
        "key3": {
            inStock: boolean;
        };
        "key4": {
            inStock: boolean;
        };
        "key5": {
            inStock: boolean;
        };
        ... 6 more ...;
        "key5": {
            ...;
        };
    };
    inStock: boolean;
}

I need to chains.fitler(x=>x.inStock) bool statement then return the specific key, i also want to add conditions to return all if none are true.

EX: key1, key5 | inStock:true return key1,key5

Advertisement

Answer

Not 100% sure how you want the input and output to be but you should get a bit closer with this implementation:

let chains = {
    keys: {
        "key1": {
            inStock: true
        },
        "key2": {
            inStock: true
        },
        "key3": {
            inStock: true
        },
        "key4": {
            inStock: true
        },
        "key5": {
            inStock: false
        }
    },
    inStock: true
}

const getInStockItems = (allKeys) => {
    const result = []
    for (let [key, value] of Object.entries(allKeys)) {
        if(value.inStock) result.push(key)
    }
    return result;
}

getInStockItems(chains.keys) // key1, key2, key3, key4 (not key5 since it's not in stock)
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement