[javascript]從陣列刪除特定的元素(delete specific element from array)

  • 119
  • 0

[javascript]從陣列刪除特定的元素(delete specific element from array)

參考於網路,自己備份一下

//從陣列刪除特定item的方式
var array = [2, 5, 9];

console.log(array);

array = removeItemFromArr(array, 9);

console.log(array); 

function removeItemFromArr(originalAttendeeArr, ele) {
    const index = originalAttendeeArr.indexOf(ele);
    if (index > -1) {
        originalAttendeeArr.splice(index, 1);
    }

    return originalAttendeeArr;
}


執行結果:




參考資料:
javascript - How can I remove a specific item from an array? - Stack Overflow