aPaulT
0
Q:

javascript remove variable from array

//Remove specific value by index
array.splice(index, 1);
11
var colors = ["red","blue","car","green"];
var carIndex = colors.indexOf("car");//get  "car" index
//remove car from the colors array
colors.splice(carIndex, 1); // colors = ["red","blue","green"]
14
// 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
var inputArray = [5, 2, 7, 24, 75];
var valueToRemove = 7;
var outputArray = [];
for (let i = 0; i < inputArray.length; i++) {
  if (inputArray[i] !== valueToRemove) {
    outputArray.push(inputArray[i]);
  }
}
console.log(outputArray); // [5, 2, 24, 75]
-1

New to Communities?

Join the community