L.Day
0
Q:

remove elemtns from an array with splice

// remove element at certain index without changing original
let arr = [0,1,2,3,4,5]
let newArr = [...arr]
newArr.splice(1,1)//remove 1 element from index 1
console.log(arr) // [0,1,2,3,4,5]
console.log(newArr)// [0,2,3,4,5]
10
fruits = ['Banana', 'Orange', 'Apple', 'Mango'];

removeFruitByIndex(index: number) {
  this.fruits = [
    ...this.fruits.slice(0, i),
    ...this.fruits.slice(i + 1, this.fruits.length),
  ];
}


removeFruitByValue(fruite: string) {
  const i = this.descriptionsList.indexOf(fruite);
  this.fruits = [
    ...this.fruits.slice(0, i),
    ...this.fruits.slice(i + 1, this.fruits.length),
  ];
}
1
var fruits = ["Banana", "Orange", "Apple", "Mango", "Kiwi"];
document.getElementById("demo").innerHTML = fruits;

function myFunction() {
  fruits.splice(2, 2);
  document.getElementById("demo").innerHTML = fruits;
}
0
const items = ['a', 'b', 'c', 'd', 'e', 'f']
const i = 2
const filteredItems = items.slice(0, i).concat(items.slice(i + 1, items.length))
// ["a", "b", "d", "e", "f"]
-1

New to Communities?

Join the community