Skip to content

Instantly share code, notes, and snippets.

View elliotlarson's full-sized avatar

Elliot Larson elliotlarson

View GitHub Profile
const characterToRemove = "Donald";
const index = characters.findIndex(c => c === characterToRemove);
const newCharacters = { ...characters };
newCharacters["1"] = newCharacter;
const newCharacters = Object.assign(characters, {
 [updateId]: updatedCharacter
});
const newCharacters = { ...characters, [updateId]: updatedCharacter };
const updateId = 1;
const updatedCharacter = { id: 1, firstName: "The", lastName: "Dude" };
const newCharacters = characters.filter(c => c !== oldName);
newCharacters.push(newName);
// gives you
// => ["Walter", "Donald", "The Dude"]
const newCharacters = [...characters];
newCharacters[index] = newName;
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;
});
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
];
const index = characters.findIndex(c => c === oldName);