【问题标题】:Remove an item when user deselect a flatlist item当用户取消选择平面列表项目时删除项目
【发布时间】:2020-08-04 23:25:00
【问题描述】:

我知道标题不够清楚,但我会在这里尝试解释更多。 我有一个平面列表,我必须选择多个项目并创建一个字符串值,其中包含我必须发送到服务器的项目 ID 和项目价格。 这是我的方法:(效果很好)

  selectItem = data => {
    let test = 130;
    if (data.Unites < test) {
      data.Etat = 0
      console.warn('points insufisants');
    } else {
      data.isSelect = !data.isSelect;
      data.selectedClass = data.isSelect ? StylesGift.validChek : StylesGift.inValidChek;
      const newItem = {
        idCadeau: data.idCadeau,
        Unites: data.Unites,
      };

      if (data.isSelect) {
        const index = this.state.paramsSend.indexOf([newItem]);

        this.setState({
          paramsSend: this.state.paramsSend.concat([newItem]),
        }, () => {
          let str = this.state.paramsSend.map(v => Object.values(v).join('_')).join(',')
          this.setState({
            params: str,
          });
        });
      }else if (data.isSelect === false) {
        const index = this.state.paramsSend.indexOf([newItem]);
        if (index > -1) {
          array.splice(index, 1);
          let str = this.state.paramsSend.map(v => Object.values(v).join('_')).join(',')
          this.setState({
            params: str,
          });
        }

   

        console.log('looog' + this.state.paramsSend);
      }
    }
    this.setState({changed: !this.state.changer})

  };

我的问题是当我取消选择一个项目时,我无法使用此代码删除该项目的 ID 和价格:

    else if (data.isSelect === false) {
        const index = this.state.paramsSend.indexOf([newItem]); <-- =-1
        if (index > -1) {
        this.state.paramsSend.splice(index, 1);
let str=this.state.paramsSend.map(v=>Object.values(v).join('_')).join(',')
              this.setState({
                params: str,
              });
            }
            console.log('looog' + this.state.paramsSend);
          }

有什么建议吗? indexOf 返回 -1 不知道为什么

const index = this.state.paramsSend.indexOf([newItem]);
console.warn('index ' + index);
console.warn('from this ' +JSON.stringify(this.state.paramsSend));
console.warn('remove this ' + JSON.stringify(newItem));

newItem 已经在 paramsSend 但它返回 -1 !

【问题讨论】:

  • array.splice() 可以从数组中删除项目,那么您哪里出错了?是index === -1?
  • 它不会删除任何东西
  • 哦,是的,当我记录索引时它给了我-1

标签: javascript arrays react-native object


【解决方案1】:

你有 2 个问题:

  1. 不要使用数组调用indexOf,而只需使用您要查找的元素:const index = this.state.paramsSend.indexOf(newItem);
  2. 仅当 item 参数是数组中的实际对象而不是副本时,在对象数组上调用的 indexOf(item) 才有效。即这不起作用:const index = this.state.paramsSend.indexOf({ idCadeau: 5, Unites: 180 }); 即使数组中存在相同的对象

所以这应该可以解决这两个问题:

const index = this.state.paramsSend.findIndex(item => item.idCadeau === newItem.idCadeau)
if (index > -1) {
  // good!
}

【讨论】:

  • 效果更好,我只需将 item.id 更改为 item.idCadeau ,非常感谢
【解决方案2】:

您的代码不是很清楚,但请尝试使用过滤功能。

它将所有匹配条件的元素推入一个数组

const newDataArray = data.filter(elem => elem.idCadeau !== newItem.idCadeau)

所以在这里,您有一个新数组,其中所有元素都与您的 newItem 不同

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 1970-01-01
    • 2014-01-22
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    • 2018-10-04
    相关资源
    最近更新 更多