Node JS Find and Update Object in Array Example

About Us:

We are from free coder. We are a group of web developers who are passionate about web development. You can learn from us php, laravel, node js, vue js, react js and many other topic on web developement.
we are very happy to help you.
free coder

Today what you are going to learn:

This post will give you lesson of nodejs find and update object in array. This article will give you simple lesson 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 lesson with solution.

In this post, i will give you one simple lesson how to find object from key or id inside array and update that object value. let's see bellow lesson:

Example:

server.js

myArray = [

{ id:1, name:'Sagor'},

{ 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);

Result:-

[

{ id: 1, name: 'Sagor' },

{ id: 2, name: 'Paresh Updated' },

{ id: 3, name: 'Rakesh' },

{ id: 4, name: 'Mahesh' }

]

thank you very much for following up with me.

Tags

Recent Posts

Related Posts