Immutable operations
// Immutable array
//add new item
const newCharacters = [ ...characters, 'Luke' ];
// Removing Vader
const withoutVader = characters.filter(char => char !== 'Vader');
// Changing Vader to Anakin
const backInTime = characters.map(char => char === 'Vader' ? 'Anakin' : char);
// Merging two character sets
const moreCharacters = [ ...characters, ...otherCharacters ];
// All characters uppercase
const shoutOut = characters.map(char => char.toUpperCase());
// sort
const sortedCharacters = characters.slice().sort();
// Immutable objects
// add new item and merge of items
const newPerson = {
...person,
age: 30
};
// delete item
const newPerson = Object.keys(person).reduce((obj, key) => {
if (key !== property) {
return { ...obj, [key]: person[key] }
}
return obj
}, {});