0

Hi i need some help to update the userSettings variable, for example if i remove a product of the products array, i need to update the sortedProducts array of the userSettings.categories array, i'm trying with nested for loops but i would like to improve performance with functional array methods. This is what i've been trying, thanks in advance community.

let products = [
        {id: 1, name: 'Brasilian', category: 'cofee'},
        {id: 2, name: 'Colombian', category: 'cofee'},
        {id: 3, name: 'Apple', category: 'fruit'},
        {id: 4, name: 'Strawberry', category: 'fruit'},
        {id: 5, name: 'Banana', category: 'fruit'},
        {id: 6, name: 'Pepper', category: 'spices'},
        {id: 7, name: 'Salt', category: 'spices'}
    ]
    
let userSettings = {
    categories: [
        {name: 'fruit', sortedProducts: [5, 3, 4]},
        {name: 'spices', sortedProducts: []},
        {name: 'cofee', sortedProducts: []},
    ]
}

// lets remove the strawberry product
products.splice(3, 1);
console.log(products);


// i need to update userSettings
const updateUserSettings = (() => {

    for(let i = 0; i < userSettings.categories.length; i++){

        if(userSettings.categories[i].sortedProducts.length){
            console.log(userSettings.categories[i].sortedProducts);

            for(let j = 0; j < products.length; j++){
                if(products[j].category == userSettings.categories[i] && !userSettings.categories[i].sortedProducts.includes(products[j].id)){
                    console.log('no includes')
                }
            }
      
        }
    }

})();






expectedOutput = {
    categories: [
        {name: 'fruit', sortedProducts: [5, 3]},
        {name: 'spices', sortedProducts: []},
        {name: 'cofee', sortedProducts: []},
    ]
}

1 Answer 1

1

Since the other categories need to have empty arrays, the best way would be to remove any existing sortedProducts in userSettings that no longer exist in products.

userSettings.categories.forEach(category => {
    // get the product ids for the category
    let filteredProductIds = products.filter(product => product.category === category.name)
        .map(product => product.id)
    // remove any id that no longer exists in products from sortedProducts
    category.sortedProducts = category.sortedProducts.filter(sortedProduct => filteredProductIds.includes(sortedProduct))
})
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your answer, i have this error in console: Cannot read property 'find' of undefined
@sonEtLumiere fixed the typos. Apologies for the sloppy answer.
sortedProducts of spices and cofee must be an empty array as the original
@sonEtLumiere is there a reason for that? Because they are clearly in the product array.
yes i need to get the expected output, spices and coffee with sortedProducts with empty arrays
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.