Skip to content

Instantly share code, notes, and snippets.

@emranweb
Created December 26, 2019 19:43
Show Gist options
  • Save emranweb/694227ed79c8a723836b422cbb8c096d to your computer and use it in GitHub Desktop.
Save emranweb/694227ed79c8a723836b422cbb8c096d to your computer and use it in GitHub Desktop.
/*
For Each - forEach accept a function iterator that loop through each item in an array
Example
*/
const numbers = [1,2,3,4,5];
numbers.forEach(element => {
console.log(element);
});
/*
Map - Map will loop through each item of array, same like forEach but Map returns the value of the array.
*/
// square of the actual numbers
let doubleNumbers = numbers.map(element => Math.pow(element,2));
console.log(doubleNumbers);
/*
Find - find will return the record if a particular element is found in the array.
*/
const students = [
{ regNo: 192, name: "Abc" },
{ regNo: 1302, name: "bcd" }
]
let findStudent = students.find(element=> element.regNo === 192);
console.log(findStudent)
/*
Filter - Filter return array based on the boolean of the comparison
*/
let largeNumbers = numbers.filter(element => element>3);
console.log(largeNumbers);
/*
Some- some accept an iterator function that will return true or false
*/
const names = ["Alemran","Jimmy","Johnny"];
const newName = names.some(element => element.length < 3)
console.log(newName)
/*
Every - The every helper is used to find whether all the elements in a given list pass a specific condition
*/
const allname = names.every(element => element.length > 4)
console.log(allname)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment