How to Remove Element from Array in Node JS?
Hi,
If you need to see example of how to remove element from array in nodejs. we will help you to give example of node js remove object from array. i explained simply about node js remove element from array. We will look at example of how to remove object from array in node js.
Here, i will give you three simple example to remove element from array using key and value. so, let's see bellow example how to push object in array in node js app.
Example 1: Node Remove Element from Array by Index
server.js
myArray = [1, 2, 3, 4, 5];
var index = 2;
myArray.splice(index, 1);
console.log(myArray);
Output:
[ 1, 2, 4, 5 ]
Example 2: Node Remove Element from Array by Value
server.js
myArray = [1, 2, 3, 4, 5];
var value = 4;
myArray.splice(myArray.indexOf(), 1);
console.log(myArray);
Output:
[ 1, 2, 4, 5 ]
Example 3: Node Delete Item from Array by Object
server.js
myArray = [
{ id:1, name:'Hardik'},
{ id:2, name:'Paresh'},
{ id:3, name:'Rakesh'},
{ id:3, name:'Mahesh'},
];
id = 2;
myArray = myArray.filter(item => item.id !== id);
console.log(myArray);
Output:
[
{ id: 1, name: 'Hardik' },
{ id: 3, name: 'Rakesh' },
{ id: 3, name: 'Mahesh' }
]
i hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to use Underscore JS in Node JS App?
- Node JS Sort Array of Objects by Value Example
- Node JS Foreach Loop Array Example
- How to Create and Use .env File in Node JS?
- Axios HTTP requests in Node JS Example
- Node JS Http Request with Headers Example
- Node JS Make HTTP Put Request Example
- Node JS Make HTTP Delete Request Example
- Node JS Make Http Get Request with Parameters Example
- Node JS Multer Rename Uploaded File Example
- Four Reasons Why Node.js is your Server-side Hero