Skip to content
the poor coder

find and replace value in array of objects javascript

Poor Man
Published
Reading time
3 min read
On this page

Find and Replace Value in Array of Objects Javascript#

As a developer, you may come across a situation where you need to find and replace a specific value in an array of objects in Javascript. This could be due to various reasons such as updating user information, making changes in data, or any other business logic. There are multiple ways to achieve this, and we will discuss some of them below.

Method 1: Using For Loop#

The simplest and most straightforward way to find and replace a value in an array of objects is by using a for-loop. Here's how you can do it:

Text

// Sample array of objects
const users = [
  { id: 1, name: 'John', age: 25 },
  { id: 2, name: 'Jane', age: 30 },
  { id: 3, name: 'Bob', age: 35 }
];

// Find and replace value
for (let i = 0; i < users.length; i++) {
  if (users[i].id === 2) {
    users[i].name = 'Janet';
    break;
  }
}

console.log(users);
// Output: [
//   { id: 1, name: 'John', age: 25 },
//   { id: 2, name: 'Janet', age: 30 },
//   { id: 3, name: 'Bob', age: 35 }
// ]

In the code above, we first define an array of objects called users. Then, we iterate over the array using a for-loop and check if the id property of any object matches our search value (in this case, 2). If it does, we replace the name property with the new value (in this case, 'Janet') and exit the loop using the break statement. Finally, we log the updated array to the console.

Method 2: Using Array.map() and Conditional Operator#

Another way to find and replace a value in an array of objects is by using the Array.map() method along with the conditional (ternary) operator. Here's an example:

Text

// Sample array of objects
const users = [
  { id: 1, name: 'John', age: 25 },
  { id: 2, name: 'Jane', age: 30 },
  { id: 3, name: 'Bob', age: 35 }
];

// Find and replace value
const updatedUsers = users.map(user => user.id === 2 ? {...user, name: 'Janet'} : user);

console.log(updatedUsers);
// Output: [
//   { id: 1, name: 'John', age: 25 },
//   { id: 2, name: 'Janet', age: 30 },
//   { id: 3, name: 'Bob', age: 35 }
// ]

In this code, we first define an array of objects called users. Then, we use the Array.map() method to iterate over each object in the array. Inside the map function, we check if the id property of the current object matches our search value (in this case, 2). If it does, we use the spread operator to create a new object with the updated name property and return it. If it doesn't, we simply return the original object. Finally, we log the updated array to the console.

These are just two of the many ways you can find and replace a value in an array of objects in Javascript. Choose the one that suits your needs the most and implement it in your code.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC