假設(shè)我有一個回應(yīng)我正在循環(huán)它并將 pic.id 推送到文件數(shù)組上files = []response.data[0].pics.forEach(function(pic, i){ let div = makeButtons(response.data[0].pics, files, imgPrevEdit); files.push(pic.id);});所以推送文件數(shù)組后會變成這樣make 按鈕函數(shù)用于使用 splice 函數(shù)刪除數(shù)組上的某個元素makeButtons: function(file, store, elem) { let button = document.createElement("button"); button.classList.add('position-absolute','x', 'rounded-circle'); button.type = "button"; button.innerHTML= "x"; button.addEventListener('click', () => { elem.removeChild(div); store.splice(store.indexOf(file), 1); }); let div = document.createElement("div"); div.classList.add('col', 'mt-2'); div.appendChild(button); return div;},但是,當(dāng)我單擊按鈕時,它總是從文件數(shù)組的末尾開始,它找不到特定的鍵并將其刪除知道我該如何解決嗎?
使用拼接從數(shù)組中刪除元素
梵蒂岡之花
2021-10-14 16:59:04