Answers for "remove items from an array javascript"

282

javascript array remove element

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"]
Posted by: Guest on July-19-2019
11

remove array elements javascript

let value = 3

let arr = [1, 2, 3, 4, 5, 3]

arr = arr.filter(item => item !== value)

console.log(arr)
// [ 1, 2, 4, 5 ]
Posted by: Guest on June-07-2020
15

remove element from array in js

var myArray = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

//removing element using splice method -- 
//arr.splice(index of the item to be removed, number of elements to be removed)
//Here lets remove Sunday -- index 0 and Monday -- index 1
  myArray.splice(0,2)

//using filter method
let itemToBeRemoved = ["Sunday", "Monday"]
var filteredArray = myArray.filter(item => !itemToBeRemoved.includes(item))
Posted by: Guest on April-30-2020
0

Remove element from array

const apps = [
  {id:1, name:'test1'}, 
  {id:2, name:'test2'},
  {id:3, name:'test3'}
]
//remove item with id=2
const itemToBeRemoved = {id:2, name:'test2'}
apps.splice(apps.findIndex(a => a.id === itemToBeRemoved.id) , 1)
//print result
console.log(apps)
Posted by: Guest on September-30-2021
0

delete an item from array javascript

let items = [12, 548 ,'a' , 2 , 5478 , 'foo' , 8852, , 'Doe' ,2154 , 119 ]; 
items.length; // return 11 
items.splice(3,1) ; 
items.length; // return 10 
/* items will be equal to [12, 548, "a", 5478, "foo", 8852, undefined × 1, "Doe", 2154,       119]   */
Posted by: Guest on November-20-2020

Code answers related to "remove items from an array javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language