how to remove a value from an array and return the new array in javascript
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); 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); let someArray = [
{name:"Kristian", lines:"2,5,10"},
{name:"John", lines:"1,19,26,96"},
{name:"Kristian", lines:"2,58,160"},
{name:"Felix", lines:"1,19,26,96"}
];
someArray = someArray.filter(person => person.name != 'John');
It will remove John!
var arr = [1, 2, 3, 4, 5, 5, 6, 7, 8, 5, 9, 0];
for( var i = 0; i < arr.length; i++){
if ( arr[i] === 5) {
arr.splice(i, 1);
i--;
}
}
//=> [1, 2, 3, 4, 6, 7, 8, 9, 0]
Also in JavaScript:
- nextjs starter template with auth
- how to make a rectangle in matter.js
- how to use style in react js
- vue methods v-on =!
- trigger click on checkbox jquery
- javascript bind multiple arguments
- jquery enforce important
- express request path
- express-jwt error algorithms should be set
- discord js bot leave voice channel
- javascript change button text onclick
- dom ready js
- js functions inside of objects
- javascript to get value of dropdown
- jquery ajax on fail
- javascript change frame background
- laravel return response json
- string to char array javascript
- prime numbers javascript
- react hook s
- javascript sendredirect
- angular model output only
- reload page javascript
- react supported events