繁体   English   中英

Splice Array 是一个空值

[英]Splice Array is rest a null value

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

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

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

{"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 "}" 被放置到位,我希望没有放置任何东西。

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

    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
    })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM