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 characters = ["Walter", "Jeffrey", "Donald"]; | |
const oldName = "Jeffrey"; | |
const newName = "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[maude.id] = maude; |
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, { [maude.id]: maude }); |
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, [maude.id]: maude }; |
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 characters = { | |
1: { id: 1, firstName: "Jeffrey", lastName: "Lebowski" }, | |
2: { id: 2, firstName: "Walter", lastName: "Sobchak" }, | |
3: { id: 3, firstName: "Donald", lastName: "Kerabatsos" } | |
}; | |
const maude = { id: 4, firstName: "Maude", lastName: "Lebowski" }; |
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
// Adding Maude after Jeffrey at index 2 | |
const newCharacters = [...characters]; | |
newCharacters.splice(2, 0, maude); | |
// => ["Walter", "Jeffrey", "Maude", "Donald"] |
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
// Creates a new array including the new item, turns it into a Set, | |
// which removes duplicates, and then turns it back to an array | |
const newCharacters = [...new Set([...characters, "Donald"])]; | |
// Since "Donald" is a non-unique item, newCharacters will be: | |
// => ["Walter", "Jeffrey", "Donald"] |
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
// creating a copy with `slice` | |
const newCharacters = characters.slice(); | |
// you could also just copy with the spread | |
// const newCharacters = […characters]; | |
newCharacters.push(maude); |
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.concat(maude); |
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, maude]; |