In your way to work in Javascript, you will might be needed to add or remove item from javascript array. You can simply add item into array with push() method.
In javascript, you can not directly remove item from array.xaFirst you need to find item index with indexOf()
method in the array and need to remove that item. Here is the example.
var arr = [1, 3, 5, 7, 11];
var item = 7;
function removeItem(array, item) {
if (array.indexOf(item) !== -1) {
array.splice(array.indexOf(item), 1);
}
return array;
}
console.log(removeItem(arr, item)); // [1, 3, 5, 11]
There is filter()
method which can do same job as above function.
var arr = [1, 3, 5, 7, 11];
var item = 7;
var newArray = arr.filter(function(e) { return e !== item });
console.log(newArray);
There are also other ways you can do with custom functions.
I hope it will help you.
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]
How to reverse the order of an array in PHP
Use the PHP array_reverse() fu...Face Detection using Webcam in PHP
In this article, we will share with you...How to Remove a Property from a JavaScript Object
Use the delete Operator You...How to get the Client IP Address in PHP
When working in one of my project, I wan...Laravel 8 Create Custom Service Provider Example
In Laravel application, you have noticed...