【问题标题】:Splice Array is rest a null value拼接数组是一个空值
【发布时间】:2020-01-02 11:43:08
【问题描述】:

我正在使用此代码从 React Native 中的 Array 中删除一个项目:

var array = [...this.state.gallery];
if (index !== -1) {
   array.splice(index, 1);
   this.setState({gallery: array,
   photosHaveNow: this.state.photosHaveNow - 1});
}

但是,当项目被删除时,该数组将写入您的位置:

{"empty": true, "key": "blank-2"}

最后一个例子:

["http://parquedasfeiras.online/wp-content/uploads/2019/09/WhatsApp-Image-2019-11-30-at-00.38.33.jpeg", "http://parquedasfeiras.online/wp-content/uploads/2019/09/WhatsApp-Image-2019-11-30-at-00.38.33-1.jpeg", "http://parquedasfeiras.online/wp-content/uploads/2019/09/WhatsApp-Image-2019-11-30-at-00.38.33-2.jpeg", "http://parquedasfeiras.online/wp-content/uploads/2019/09/WhatsApp-Image-2019-11-30-at-00.38.33-3.jpeg", "http://parquedasfeiras.online/wp-content/uploads/2019/09/WhatsApp-Image-2019-11-30-at-00.38.34.jpeg", "http://parquedasfeiras.online/wp-content/uploads/2019/09/WhatsApp-Image-2019-11-30-at-00.38.34-1.jpeg", "http://parquedasfeiras.online/wp-content/uploads/2019/09/WhatsApp-Image-2019-11-30-at-00.38.34-4.jpeg", {"empty": true, "key": "blank-2"}, {"empty": true, "key": "blank-2"}, {"empty": true, "key": "blank-2"}, {"empty": true, "key": "blank-2"}, {"empty": true, "key": "blank-2"}]

当通过拼接移除一个项目时,“{”empty“:true,”key“:”blank-2“}”放置到位,我希望什么都没有放置。

【问题讨论】:

  • 问题不清楚。你想用那个对象替换删除的项目吗?或其他任何东西。请清楚地编辑问题。提供要处理的数据。
  • 你好,抱歉当通过拼接移除一个项目时,"{" empty ": true," key ":" blank-2 "}" 放置到位,我希望什么都没有放入地点。

标签: javascript reactjs react-native


【解决方案1】:

我相信我已经找到了解决方案。不知道好不好听。

    var array = [...this.state.gallery];

    if (index !== -1) {
        array.splice(index, 1);
    }

    var finalArray = [];

    array.forEach(element => {
        if(typeof element !== 'object')
        {
            finalArray = finalArray.concat(element)
        }
    })

    this.setState({
        gallery: finalArray,
        photosHaveNow: this.state.photosHaveNow - 1
    })

【讨论】:

  • 好吧,这是非常错误的。您正在将该对象推送到代码中的某个位置的数组中。所以找到并修复那段代码。可能在你的“deleteItem”函数中。
猜你喜欢
  • 2018-09-09
  • 2017-03-11
  • 2015-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多