Node JS Find and Update Object in Array Example

By Hardik Savani May 28, 2021 Category : Node JS

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...

Tags :
Shares