我在从另一个数组拼接元素然后更改状态时更改状态有问题。需要明确的是,我不想改变状态,只想从数组arrayWithFilters = []中拼接一个元素

export const SHOW_RESULTS_WHEN_NOTHING = (state) => {
  let selectedFilters = {...state.selected.filters},
  arrayWithFilters = []; //Making splice from this array, but it also removes from 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)
    }
  })
}

最佳答案

下一个代码解决了问题

export const SHOW_RESULTS_WHEN_NOTHING = (state) => {
let selectedFilters = {...state.selected.filters},
    arrayWithFilters = [];

let selectedFiltersCopy = JSON.parse(JSON.stringify(selectedFilters));

arrayWithFilters = Object.values(selectedFiltersCopy);

arrayWithFilters.forEach((filter) => {

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

        filter.values.splice('qcontrast', 1);

    }
})

console.log(arrayWithFilters)


}

10-06 04:39