簡體   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