Last active
August 18, 2020 18:52
-
-
Save mreigen/ec8f7767944c7558d9c368755cb1580f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let array = [ | |
{name: "Minh", age: 20}, | |
{name: "Brian", age: 22}, | |
{name: "Hugo", age: 12}, | |
{name: "Zelda", age: 56}, | |
{name: "Simon", age: 7} | |
]; | |
const nameToFind = "Hugo"; | |
const personToReplace = {name: "Ali", age: 34}; | |
const index = array.findIndex(item => item.name === nameToFind); | |
Object.assign([], array, { [index]: personToReplace }); | |
//=> [ | |
// {name: "Minh", age: 20} | |
// {name: "Brian", age: 22} | |
// {name: "Ali", age: 34} <--------- | |
// {name: "Zelda", age: 56} | |
// {name: "Simon", age: 7} | |
//]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment