P
P
pankratov_tolik2019-05-30 09:38:39
JavaScript
pankratov_tolik, 2019-05-30 09:38:39

Why does the data in State change when an element is removed from another array?

I don’t want the state to change, but I can’t understand what is the relationship of the state with this array

export const SHOW_RESULTS_WHEN_NOTHING = (state) => {
    let selectedFilters = {...state.selected.filters},
        arrayWithFilters = []; //Я удаляю элемент с этого массива, ниже в фориче, и тот же элемент удаляется из объекта стейте state.selected.filters

    for (let filter in selectedFilters) {
        arrayWithFilters.push(selectedFilters[filter])
    }

    arrayWithFilters.forEach((filter) => {

        if (filter.values.includes('qcontrast')) {

            filter.values.splice('qcolor', 1);
            console.log(filter)

        }
    })

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Robur, 2019-05-30
@Robur

you are not deleting from the array, but from the object that lies in the array (filter.values). The same object lies in another array, wherever you change it, it will change everywhere.
Make a complete copy of each element in arrayWithFilters.push(...).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question