How can I remove a specific item from an array?
Find the index
of the array element you want to remove using indexOf
, and then remove that index with splice
.
The splice() method changes the contents of an array by removing
existing elements and/or adding new elements.
const array = [2, 5, 9];
console.log(array);
const index = array.indexOf(5);
if (index > -1) { // only splice array when item is found
array.splice(index, 1); // 2nd parameter means remove one item only
}
// array = [2, 9]
console.log(array);
How to remove item from array by value?
This can be a global function or a method of a custom object, if you aren't allowed to add to native prototypes. It removes all of the items from the array that match any of the arguments.
Array.prototype.remove = function() {
var what, a = arguments, L = a.length, ax;
while (L && this.length) {
what = a[--L];
while ((ax = this.indexOf(what)) !== -1) {
this.splice(ax, 1);
}
}
return this;
};
var ary = ['three', 'seven', 'eleven'];
ary.remove('seven');
/* returned value: (Array)
three,eleven
*/
To make it a global-
function removeA(arr) {
var what, a = arguments, L = a.length, ax;
while (L > 1 && arr.length) {
what = a[--L];
while ((ax= arr.indexOf(what)) !== -1) {
arr.splice(ax, 1);
}
}
return arr;
}
var ary = ['three', 'seven', 'eleven'];
removeA(ary, 'seven');
/* returned value: (Array)
three,eleven
*/
And to take care of IE8 and below-
if(!Array.prototype.indexOf) {
Array.prototype.indexOf = function(what, i) {
i = i || 0;
var L = this.length;
while (i < L) {
if(this[i] === what) return i;
++i;
}
return -1;
};
}
How can I remove a specific item from an array?
Find the index
of the array element you want to remove using indexOf
, and then remove that index with splice
.
The splice() method changes the contents of an array by removing
existing elements and/or adding new elements.
const array = [2, 5, 9];
console.log(array);
const index = array.indexOf(5);
if (index > -1) { // only splice array when item is found
array.splice(index, 1); // 2nd parameter means remove one item only
}
// array = [2, 9]
console.log(array);
How can I remove an array element by index,using javaScript?
You can use splice
to do the same. Syntax = array.splice(start_index, no_of_elements)
Following is the command:
const fruits = ["mango","apple","pine","berry"]; // returns mutated array
const removed = fruits.splice(2, 1); // returns array of removed items
console.log('fruits', fruits);
console.log('removed', removed);
How can I remove a specific item from an array?
Find the index
of the array element you want to remove using indexOf
, and then remove that index with splice
.
The splice() method changes the contents of an array by removing
existing elements and/or adding new elements.
const array = [2, 5, 9];
console.log(array);
const index = array.indexOf(5);
if (index > -1) { // only splice array when item is found
array.splice(index, 1); // 2nd parameter means remove one item only
}
// array = [2, 9]
console.log(array);
How to delete an item from state array?
To remove an element from an array, just do:
array.splice(index, 1);
In your case:
removePeople(e) {
var array = [...this.state.people]; // make a separate copy of the array
var index = array.indexOf(e.target.value)
if (index !== -1) {
array.splice(index, 1);
this.setState({people: array});
}
},
How can I remove a specific item from an array?
Find the index
of the array element you want to remove using indexOf
, and then remove that index with splice
.
The splice() method changes the contents of an array by removing
existing elements and/or adding new elements.
const array = [2, 5, 9];
console.log(array);
const index = array.indexOf(5);
if (index > -1) { // only splice array when item is found
array.splice(index, 1); // 2nd parameter means remove one item only
}
// array = [2, 9]
console.log(array);
Remove a specific item from an array of objects Reactjs
You need to pass the ticketID
to your IconButton
's onClick
handler:
<IconButton
onClick={() => removeTicket(row.ticketID)}
aria-label="delete"
color="primary"
>
<DeleteIcon />
</IconButton>
And update your handler where you can create a new array using the .filter()
method:
const removeTicket = (ticketID)=> {
setRows(rows.filter(item => ticketID !== item.ticketID)
}
This rows.filter()
returns every element where the ticketID
is not the same as the one you passed down.
Removing a specific element from an array by name in AS3
Documentation of splice()
Both parameters need to be integers, the first one is the position of the element you want to delete, and the second one is the amount of elements you want to delete. Try myarray.splice(myarray.indexOf(myclip),1);
Don't know why it would only remove the first element in your snippet, maybe internally it casts myclip
to 0? Doesn't matter, use indexOf
. If that doesn't work, for loop through the array to get the position first.
Remove first Item of the array (like popping from stack)
The easiest way is using shift()
. If you have an array, the shift
function shifts everything to the left.
var arr = [1, 2, 3, 4];
var theRemovedElement = arr.shift(); // theRemovedElement == 1
console.log(arr); // [2, 3, 4]
Related Topics
How to Embed an Autoplaying Youtube Video in an Iframe
How to Detect a Mobile Device With JavaScript
Getting Binary (Base64) Data from Html5 Canvas (Readasbinarystring)
Text-Overflow Ellipsis on Left Side
Es6 Modules in the Browser: Uncaught Syntaxerror: Unexpected Token Import
Alternative For Deprecated Svg Pathseglist
Html ≪Select≫ Selected Option Background-Color CSS Style
Convert Svg to Png With Applied Images as Background to Svg Elements
Jquery: Get Selected Element Tag Name
Pass Mouse Events Through Absolutely-Positioned Element
In Jquery, How to Select an Element by Its Name Attribute
How to Bind Events on Ajax Loaded Content
How to Use a Variable For a Key in a JavaScript Object Literal
How to Return the Response from an Observable/Http/Async Call in Angular
Accessing Nested JavaScript Objects and Arrays by String Path
How to Deal With Floating Point Number Precision in JavaScript