Redux比较每个数组索引

时间:2017-11-03 18:38:06

标签: javascript reactjs redux

我试图添加一个假设有一个索引数组的状态,并且每个索引必须与状态索引和商店状态的删除进行比较,例如我有一个产品的状态:

state.products = [
    {productName: "patric", withTax: "0.57", noTax: "2.30", tax: 25, quantity: 2}
    {productName: "parafuso", withTax: "55.50", noTax: "222.00", tax: 25, quantity: 2} //remove this
    {productName: "mamao", withTax: "0.57", noTax: "2.30", tax: 25, quantity: 5} // remove this
]

action.index = [
    {productName: "parafuso", withTax: "55.50", noTax: "222.00", tax: 25, quantity: 2} //remove this
    {productName: "mamao", withTax: "0.57", noTax: "2.30", tax: 25, quantity: 5} // remove this
]

    //this is not removing the selecteds items
    const products = state.products.filter((product, i) => {

        for(let i=0; i < action.index.length; i++){
            if(product.name !== action.index[i].name) return true
        }

        return false
    }) 
    return {
        ...state,
        products
    } 

3 个答案:

答案 0 :(得分:1)

我会采取类似但优化的方法。

首先,我只想提取您要删除的产品的名称,因为运行查找该州所有产品的项目是没有意义的。

其次,由于浏览器可以优化执行,因此使用indexOf而不是具有相等运算符for的{​​{1}}循环执行速度更快。

最后,尝试提高可读性。

===

修改

您也可以将// Given that this is your state state.products [ {productName: "patric", withTax: "0.57", noTax: "2.30", tax: 25, quantity: 2} {productName: "parafuso", withTax: "55.50", noTax: "222.00", tax: 25, quantity: 2} //remove this {productName: "mamao", withTax: "0.57", noTax: "2.30", tax: 25, quantity: 5} // remove this ] // Given that this is the action you dispatch action = { type: 'FILTER_PRODUCTS' index: [ {productName: "parafuso", withTax: "55.50", noTax: "222.00", tax: 25, quantity: 2} //remove this {productName: "mamao", withTax: "0.57", noTax: "2.30", tax: 25, quantity: 5} // remove this ] } //Reducer code... case 'FILTER_PRODUCTS': { // extract the names just once const namesToRemove = action.index.map( product => product.name ) // filter the products const products = state.products.filter( product => namesToRemove.indexOf( product.name ) === -1 ) return { ...state, products } } 替换为较新的Array.indexOf

Array.includes

成为

const products = state.products.filter( product => namesToRemove.indexOf( product.name ) === -1 )

答案 1 :(得分:0)

使用.filter()函数,因为它返回数组的不可变数组,而不包含要删除的值。 我假设您要求从减速机内的状态中移除元素,您的问题似乎到处都是......

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

state.products [
  {name: pudim},
  {name: car}, //want to remove this
  {name: cloud} //want to remove this
]

//Reducer code...

case ProductActionType.SELECT_PRODUCT: { 
  const products = state.products.filter((product, index) => { 
    for(let i=0; i < action.productFromAction.length; i++){
        if(product.name === action.productFromAction[i].name) return false
    }
    return true
  }) 
  return { ...state, products } 
}

答案 2 :(得分:0)

如果您只想保留状态为索引的产品,可以使用过滤功能完成。

像这样

let state = {
  products: [{
      name: 'pudim'
    },
    {
      name: 'car'
    },
    {
      name: 'cloud'
    }
  ],
  index: [1,2] 
}

const withoutRemoved = state.products.filter((product, i) => state.index.indexOf(i) === -1 )

console.log( {
  ...state,
  products: withoutRemoved
})

map总是返回一个具有相同空格的数组,即使它们是“undefined