Node JS Find and Update Object in Array Example
This post will give you example of nodejs find and update object in array. This article will give you simple example of nodejs find and update object in array by key. you will learn node js update object in array. if you have question about node.js update json object then i will give simple example with solution.
In this post, i will give you one simple example how to find object from key or id inside array and update that object value. let's see bellow example:
Example:
server.js
myArray = [
{ id:1, name:'Hardik'},
{ id:2, name:'Paresh'},
{ id:3, name:'Rakesh'},
{ id:4, name:'Mahesh'},
];
id = 2;
index = myArray.map(function(x) {return x.id; }).indexOf(id);
myArray[index].name = "Paresh Updated";
console.log(myArray);
Output:
[
{ id: 1, name: 'Hardik' },
{ id: 2, name: 'Paresh Updated' },
{ id: 3, name: 'Rakesh' },
{ id: 4, 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 Remove Element from Array in Node JS?
- How to Push Element in Array in Node JS?
- 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 Express Form Submission Example
- Node JS Mysql Connection Example