Skip to content

Instantly share code, notes, and snippets.

@DaveMBush
Last active August 5, 2017 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DaveMBush/9c023a712c293fc28a72418b6bb2699d to your computer and use it in GitHub Desktop.
Save DaveMBush/9c023a712c293fc28a72418b6bb2699d to your computer and use it in GitHub Desktop.
Explaining Observables
Observable.fromEvent(this.myButton,'click').subscribe((event) =>
   /* do something in response to the click here */
);
Observable.fromEvent(this.myInput,'change').debounceTime(250).subscribe((event) =>
   /* do something in response to the input field change here */
);
var someList = [
   {id: 1, firstName: ‘Dave’, lastName: ‘Bush’},
   {id: 2, firstName: ‘John’, lastName: ‘Doe’},
   …
];
newArray = someList.filter(function(item) {
   return item.lastName.startsWith(‘B’);
}.map(function(item) {
   return {fullName: item.firstName + ‘ ‘ +
       item.lastName, id: item.id};
});
this.httpClient.get<TypeInfo>('/api/get-data')
   .subscribe((x: TypeInfo) => /* do something with the data */ );
this.httpClient.get<TypeInfo>(‘/api/get-data’)
   .catch(err: Error => /* do something with the error */)
   .subscribe((x: TypeInfo) => /* do something with the data */);
this.httpClient.get<TypeInfo>(‘/api/get-data’)
   .retry(3)
   .catch(err: Error => /* do something with the error */)
   .subscribe((x: TypeInfo) => /* do something with the data */);
var someList = [
   {id: 1, firstName: ‘Dave’, lastName: ‘Bush’},
   {id: 2, firstName: ‘John’, lastName: ‘Doe’},
   …
];
var i;
var newArray = [];
for(i = 0; i < someList.length; i++) {
   var item = someList[i];
   newArray.push({fullName: item.firstName + ‘ ‘ +
       item.lastName, id: item.id});
}
var someList = [
   {id: 1, firstName: ‘Dave’, lastName: ‘Bush’},
   {id: 2, firstName: ‘John’, lastName: ‘Doe’},
   …
];
var i;
var newArray = [];
for(i = 0; i < someList.length; i++) {
   var item = someList[i];
   if(item.lastName.startsWith(‘B’) {
       newArray.push(item);
   }
}
var someList = [
   {id: 1, firstName: ‘Dave’, lastName: ‘Bush’},
   {id: 2, firstName: ‘John’, lastName: ‘Doe’},
   …
];
var newArray = [];
someList.map(function(item) {
   if(item.lastName.startsWith(‘B’)) {
       newArray.push({fullName: item.firstName + ‘ ‘ +
           item.lastName, id: item.id});
   }
});
var someList = [
   {id: 1, firstName: ‘Dave’, lastName: ‘Bush’},
   {id: 2, firstName: ‘John’, lastName: ‘Doe’},
   …
];
newArray = someList.filter(function(item) {
   return item.lastName.startsWith(‘B’);
};
var someList = [
   {id: 1, firstName: ‘Dave’, lastName: ‘Bush’},
   {id: 2, firstName: ‘John’, lastName: ‘Doe’},
   …
];
var newArray = someList.map(function(item) {
   return {fullName: item.firstName + ‘ ‘ +
       item.lastName, id: item.id};
});
var numbers = [1, 2, 3];
var total = numbers.reduce(function(sum, item) {
   return sum + item;
}, 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment