1// Spread syntax allows an iterable (in this case an object) to be expanded
2
3const originalObj = { name: 'John', age: 34 }
4let newObj = { ...originalObj, city: 'New York' }
5// newObj is now { name: 'John', age: 34, city: 'New York' }
6
7// it can also be used with the same object
8newObj = { ...newObj, language: 'en' }
9// { name: 'John', age: 34, city: 'New York', language: 'en' }
10
1var list = [];
2
3list.push({name:'John', last_name:'Doe'});
4list.push({name:'Jane', last_name:'Doe'});
5
6console.log(list);
7/* Result:
8[
9 {
10 "name": "John",
11 "last_name": "Doe"
12 },
13 {
14 "name": "Jane",
15 "last_name": "Doe"
16 }
17]
18*/
1How about storing the alerts as records in an array instead of properties of a single object ?
2
3var alerts = [
4 {num : 1, app:'helloworld',message:'message'},
5 {num : 2, app:'helloagain',message:'another message'}
6]
7And then to add one, just use push:
8
9alerts.push({num : 3, app:'helloagain_again',message:'yet another message'});
1var alerts = [
2 {num : 1, app:'helloworld',message:'message'},
3 {num : 2, app:'helloagain',message:'another message'}
4]
5