ItSolutionStuff.com

Node JS Find and Update Object in Array Example

By Hardik Savani • May 28, 2021
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: Node JS
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Remove Element from Array in Node JS?

Read Now →

How to Push Element in Array in Node JS?

Read Now →

How to use Underscore JS in Node JS App?

Read Now →

Node JS Sort Array of Objects by Value Example

Read Now →

Node JS Foreach Loop Array Example

Read Now →

How to Create and Use .env File in Node JS?

Read Now →

Axios HTTP requests in Node JS Example

Read Now →

Node JS Http Request with Headers Example

Read Now →

Node JS Make HTTP Put Request Example

Read Now →

Node JS Express Form Submission Example

Read Now →

Node JS Mysql Connection Example

Read Now →