Skip to content

Instantly share code, notes, and snippets.

@fariswd
Last active March 8, 2019 08:56
Show Gist options
  • Save fariswd/b01c9f787b84506dfad90a842aa3440a to your computer and use it in GitHub Desktop.
Save fariswd/b01c9f787b84506dfad90a842aa3440a to your computer and use it in GitHub Desktop.
immutable groceries
// js
const groceries = {
fruit: ['apple', 'orange', 'grapes'],
};
const newGroceries = {
...groceries,
fruit: [
...groceries.fruit,
'lychee'
]
}
console.log(groceries)
console.log(newGroceries)
//dart
void main() {
Map groceries = {
'fruit': ['apple', 'orange', 'grapes'],
};
Map newGroceries = Map()
..addAll(groceries)
..update(
'fruit',
(_) => []
..addAll(groceries['fruit'])
..add('lychee'));
print(groceries);
print(newGroceries);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment