How to Find and Replace Object in an Array JavaScript?

Are you wondering how we find and replace object in an array in JavaScript? Well, read on to learn new insights.

In this article, we’ll show you several methods of how to replace object in an Array.

Are you ready to discover the secrets to finding and replacing objects in an array using JavaScript?

Let’s get started exploring several methods, including the powerful map() and splice() methods, to update your arrays with ease.

How to replace an object in an array in JavaScript?

You can replace an object in an array by first locating the object you want to replace and then updating it with the new object.

Let’s break down the process step by step:

  1. Locate the Object

To replace an object, you need to find its index in the array.

You can use methods like indexOf() or a loop to search for the object based on specific criteria.

  1. Create a New Object

Once you have identified the object to replace, create a new object with the desired changes or values.

  1. Replace the Object

Finally, replace the old object in the array with the newly created one. You can use the array index to accomplish this.

What are the different methods to replace an object in an array in JavaScript

There are several methods that we can able to use to replace an object in an array using JavaScript. Here are some methods:

Method 1: Use the map() method

The map() method in JavaScript is a powerful tool for replacing objects in an array while preserving the original array.

It creates a new array with the modified objects based on a provided callback function.

Here’s an example:

const sampleArray = [
  { id: 1, name: 'Andrea' },
  { id: 2, name: 'Bea' },
  { id: 3, name: 'Criza' },
];

const updatedArray = sampleArray.map(obj => { ✅
  if (obj.id === 1) {
    return { ...obj, name: 'Itsourcecode' };
  }
  return obj;
});

console.log(updatedArray);

In our given example, we use the map method to create a new array with the results of calling a provided function on every element in the calling array.

In this case, the function checks if the id property of the current object is equal to 1.

If it is, the function returns a new object with the same properties as the current object, but with the name property updated to ‘Itsourcecode.’

If the id property is not equal to 1, the function returns the current object unchanged.

Output:

[
  { id: 1, name: 'Itsourcecode' },
  { id: 2, name: 'Bea' },
  { id: 3, name: 'Criza' }
]

Method 2: Use the splice() method

The splice() method allows you to modify an array by removing, adding, or replacing elements.

To replace an object, specify the index and the number of elements to remove, and then add the new object.

Here’s an example:


const employee = [
  { name: 'Andrea', age: 18 },
  { name: 'Bea', age: 19 },
  { name: 'Criza', age: 20 }
];

const index = employee.findIndex(employee => employee.name === 'Andrea');
if (index !== -1) {
  employee.splice(index, 1, { name: 'Itsourcecode', age: 21 });✅
}

console.log(employee);

As you can see in our given example: we use the findIndex method to find the index of the object with the name property equal to ‘Andrea.’

If the object is found (i.e., index is not -1), the splice method is used to remove one element at the specified index and replace it with a new object { name: ‘Itsourcecode’, age: 21 }.

Output:

[
  { name: 'Itsourcecode', age: 21 },
  { name: 'Bea', age: 19 },
  { name: 'Criza', age: 20 }
]

Use a for loop with the splice method

This method allows you to iterate over each element in the array and check if it satisfies a certain condition.

If it does, you can use the splice method to remove or replace existing elements in an array at the desired index.

Here’s an example:

const employee = [
  { name: 'Andrea', age: 18 },
  { name: 'Bea', age: 19 },
  { name: 'Criza', age: 20 }
];

for (let i = 0; i < employee.length; i++) { ✅
  if (employee[i].name === 'Andrea') {
    employee.splice(i, 1, { name: 'Itsourcecode', age: 18 });
    break;
  }
}

console.log(employee);

It uses a for loop to iterate over each element in the employee array.

If the name property of the current element is equal to ‘Andrea’, the splice method is used to remove one element at the current index (i) and replace it with a new object { name: ‘Itsourcecode’, age: 18 }.

The break statement is used to exit the loop once the replacement has been made.

Output:

[
  { name: 'Itsourcecode', age: 18 },
  { name: 'Bea', age: 19 },
  { name: 'Criza', age: 20 }
]

Conclusion

Replacing an object in an array in JavaScript can be achieved through various methods, such as using the map() method, the splice() method, or a for loop with the splice method.

These methods allow you to locate the object you want to replace, create a new object with the desired changes or values, and finally replace the old object in the array with the newly created one.

We hope this article has provided you with enough information to help you understand the replace object in array JavaScript.

If you want to dive into more JavaScript topics, check out the following articles:

Thank you for reading Itsourcecoders 😊.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.
Caren Bautista

Technical Writer at PIES IT Solution

Responsible for crafting clear, well-structured, and beginner-friendly content across the platform. Handles the writing, proofreading, and editorial review of tutorials, guides, and documentation to ensure every article is accurate, readable, and easy to follow.

Expertise: Technical Writing · Content Creation · Documentation · Editorial Writing · JavaScript · TypeScript · Python · Python Errors · HTTP Errors · MS Excel  · View all posts by Caren Bautista →

Leave a Comment