remove elements from array js
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"]var data = [1, 2, 3];
// remove a specific value
// splice(starting index, how many values to remove);
data = data.splice(1, 1);
// data = [1, 3];
// remove last element
data = data.pop();
// data = [1, 2];var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
var removed = arr.splice(2,2);
/*
removed === [3, 4]
arr === [1, 2, 5, 6, 7, 8, 9, 0]
*/const array = [2, 5, 9];
console.log(array);
const index = array.indexOf(5);
if (index > -1) {
array.splice(index, 1);
}
// array = [2, 9]
console.log(array); var fruits = ["Banana", "Orange", "Apple", "Mango", "Kiwi"];
document.getElementById("demo").innerHTML = fruits;
function myFunction() {
fruits.splice(2, 2);
document.getElementById("demo").innerHTML = fruits;
}
Also in JavaScript:
- Calculate string value in javascript, not using eval
- launch.json vscode electron
- npm start for production
- setup node js express basic setup
- react native add two view
- uncaught TypeError: Bootstrap's JavaScript requires jQuery. jQuery must be included before Bootstrap's JavaScript electron
- open bootstrap modal using javascript
- clear the console while in strict mode
- remove an attribute from style tag using jquery
- nodejs store json from web api
- hot loading react native shortcut key
- how to write notes in js
- scroll down hide header jquery
- operator sum string to character javascript
- javascript merge multidimensional array
- v-switch vuex store
- how to import npm module
- es module __dirname alternative
- html select multiple selected values
- addclass to elementref angular
- how to access viewmodel in jquery
- how to compare two strings in javascript if condition
- how to use http in development mode for nodejs
- jqgrid aftershowform