This file contains hidden or 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
const characterToRemove = "Donald"; | |
const index = characters.findIndex(c => c === characterToRemove); |
This file contains hidden or 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
const newCharacters = { ...characters }; | |
newCharacters["1"] = newCharacter; |
This file contains hidden or 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
const newCharacters = Object.assign(characters, { | |
[updateId]: updatedCharacter | |
}); |
This file contains hidden or 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
const newCharacters = { ...characters, [updateId]: updatedCharacter }; |
This file contains hidden or 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
const updateId = 1; | |
const updatedCharacter = { id: 1, firstName: "The", lastName: "Dude" }; |
This file contains hidden or 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
const newCharacters = characters.filter(c => c !== oldName); | |
newCharacters.push(newName); | |
// gives you | |
// => ["Walter", "Donald", "The Dude"] |
This file contains hidden or 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
const newCharacters = [...characters]; | |
newCharacters[index] = newName; |
This file contains hidden or 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
const newCharacters = characters.map(c => (c === oldName ? newName : c)); | |
// or without the ternary | |
const newCharacters = characters.map(c => { | |
if (c === oldName) { | |
return newName; | |
} | |
return c; | |
}); |
This file contains hidden or 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
const newCharacters = [ | |
...characters.slice(0, index), // all the items before the update item | |
newName, // add the newName in place of the old name | |
...characters.slice(index + 1) // all the items after the update item | |
]; |
This file contains hidden or 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
const index = characters.findIndex(c => c === oldName); |