Skip to content

Instantly share code, notes, and snippets.

@shivenigma
Last active January 22, 2020 18:40
Show Gist options
  • Save shivenigma/01f08afce152370cb587c2f13bc6660e to your computer and use it in GitHub Desktop.
Save shivenigma/01f08afce152370cb587c2f13bc6660e to your computer and use it in GitHub Desktop.
Javascript array methods
arr.forEach(callback, [, thisArg]);
// thisArg is optional for forEeach
// the callback has the following arguments in forEach.
callback(currentValue, [, index [, array]])
// index and array are optional
const names =["Javascript", "Python", "PHP", "Flutter"];
names.forEach(name => {
console.log(name);
});
// OUTPUT
// > Javascript
// > Python
// > PHP
// > Flutter
let newArray = arr.map(callback, [, thisArg]); // thisArg is optional
callback(currentValue, [, index, [, array]]); // index and array are optional
// the callback must return an element for the new array after applying any transformation
const names =["Javascript", "Python", "PHP", "Flutter"];
const newNames = names.map((name, index) => {
return index.toString() + '. ' +name;
});
console.log(newNames);
// OUTPUT : Array ["0. Javascript", "1. Python", "2. PHP", "3. Flutter"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment