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 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"]
const cars = ['farrari', 'Ice Cream'/* It is not an car */, 'tata', 'BMW']
//to remove a specific element
cars.splice(colors.indexOf('Ice Cream'), 1);
//to remove the last element
cars.pop();
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))
// 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]
array.splice(indexToStartFrom, numbersOfItemsToBeDeleted);
// for deleting only one element
array.splice(index, 1);
//index = index of element in an array;
var colors = ["red", "blue", "car","green"];
// op1: with direct arrow function
colors = colors.filter(data => data != "car");
// op2: with function return value
colors = colors.filter(function(data) { return data != "car"});
let fruit = ['apple', 'banana', 'orange', 'lettuce'];
// ^^ An example array that needs to have one item removed
fruit.splice(3, 1); // Removes an item in the array using splice() method
// First argument is the index of removal
// Second argument is the amount of items to remove from that index and on
var colors = ["red", "orange", "yellow", "green"];
colors = colors.splice(3, 1); // Starts removing from the third element, and deletes 1 element
// Array.splice(startingElement, elementYouWantToRemove);
let numbers = [1,2,3,4]
// to remove last element
let lastElem = numbers.pop()
// to remove first element
let firstElem = numbers.shift()
// both these methods modify array while returning the removed element
// Create a function to do it easily and how many time you want!
const deleteFromArray = (array, value) => {
let newArray = array.filter(item => item !== value)
return newArray;
}
var array = [1, 2, 3, 4]
const newArr = deleteFromArray(array, 2)
console.log('Changed array:', newArr);
// Changed array: [ 1, 3, 4 ]
pop - Removes from the End of an Array.
shift - Removes from the beginning of an Array.
splice - removes from a specific Array index.
filter - allows you to programatically remove elements from an Array.
let dailyActivities = ['work', 'eat', 'sleep', 'exercise'];
// remove the last element
dailyActivities.pop();
console.log(dailyActivities); // ['work', 'eat', 'sleep']
// remove the last element from ['work', 'eat', 'sleep']
const removedElement = dailyActivities.pop();
//get removed element
console.log(removedElement); // 'sleep'
console.log(dailyActivities); // ['work', 'eat']
/*
* C program to delete an element from array at specified position
*/
#include <stdio.h>
#define MAX_SIZE 100
//this code does not literally delete an element
//this does rewrite the rest of the array over the element to be deleted
//and reduceing the count by one
//https://codeforwin.org/2015/07/c-program-to-delete-element-from-array.html
int main()
{
int arr[MAX_SIZE];
int i, size, pos;
/* Input size and element in array */
printf("Enter size of the array : ");
scanf("%d", &size);
printf("Enter elements in array : ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}
/* Input element position to delete */
printf("Enter the element position to delete : ");
scanf("%d", &pos);
/* Invalid delete position */
if(pos < 0 || pos > size)
{
printf("Invalid position! Please enter position between 1 to %d", size);
}
else
{
/* Copy next element value to current element */
for(i=pos-1; i<size-1; i++)
{
arr[i] = arr[i + 1];
}
/* Decrement array size by 1 */
size--;
/* Print array after deletion */
printf("
Elements of array after delete are : ");
for(i=0; i<size; i++)
{
printf("%d ", arr[i]);
}
}
return 0;
}