Skip to content

Instantly share code, notes, and snippets.

@adeelibr
Created November 17, 2019 21:42
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 adeelibr/46a22cef9c7a64e69678779f7cb804a6 to your computer and use it in GitHub Desktop.
Save adeelibr/46a22cef9c7a64e69678779f7cb804a6 to your computer and use it in GitHub Desktop.
Understanding javascript array methods like filter, map, some, every, reduce
// Understanding array methods:-
// - filter
// - map
// - some
// - every
// - reduce
let myOrgArray = [1,2,3, 4, 5, 6, 7, 8, 9, 10];
// Discussing array by reference
// let anotherArray = myOrgArray;
// anotherArray[0] = 9000;
// console.log('anotherArray', anotherArray);
// console.log('myOrgArray', myOrgArray);
// Filter
// const evenArray = myOrgArray.filter((currentItem, index, self) => {
// return currentItem % 2 === 0;
// });
// console.log('evenArray', evenArray);
// Map
// const doubleArray = myOrgArray.map((currentItem, index, self) => {
// return currentItem + currentItem;
// });
// console.log('myOrgArray', myOrgArray);
// console.log('doubleArray', doubleArray);
// Understanding OR, AND ops
// const condition1 = true;
// const condition2 = true;
// const condition3 = true;
// if (condition1 || condition2 || condition3) {
// console.log('I am an OR operator');
// }
// if (condition1 && condition2 && condition3) {
// console.log('I am an AND operator');
// }
// Some (OR operator)
// const doesItHave3 = myOrgArray.some((currentItem, index, self) => {
// return currentItem === 3;
// });
// console.log('doesItHave3 ::', doesItHave3);
// Every (AND operator)
// myOrgArray.push('abc');
// console.log('myOrgArray ::', myOrgArray)
// const isItANumbersArray = myOrgArray.every((currentItem, index, self) => {
// return typeof currentItem === 'number';
// });
// console.log('isItANumbersArray ::', isItANumbersArray);
// Reduce
// const myReduce = myOrgArray.reduce((accumulator, currentValue, index, self) => {
// accumulator = `${accumulator}, ${currentValue} :: ??`;
// return accumulator;
// }, '');
// console.log('myReduce', myReduce);
// --------------------------------------------------
// String literals;
// const name = 'Adeel';
// console.log('my name is' + name + ' and I like to write javascript');
// console.log(`my name is ${name} & I like to write javascript`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment