Javascript/Javascript

[자바스크립트] how to remove array element javascript

Rainbow🌈Coder 2022. 4. 17. 14:10
728x90
구글신
There are different methods and techniques you can use to remove elements from JavaScript arrays:
  1. pop - Removes from the End of an Array.
  2. shift - Removes from the beginning of an Array.
  3. splice - removes from a specific Array index.
  4. filter - allows you to programatically remove elements from an Array.

splice(시작점, 지울갯수)

 

코드 예시

function deleteTask(id) {
  for (let i = 0; i < taskList.length; i++) {
    if (taskList[i].id == id) {
      taskList.splice(i, 1);
      break;
    }
  }
  console.log("삭제");
  render();
}
728x90