Skip to content

Instantly share code, notes, and snippets.

@tararoutray
Last active September 3, 2021 15:07
Show Gist options
  • Save tararoutray/fd5b2efe669091f2d8b83dee94a32db3 to your computer and use it in GitHub Desktop.
Save tararoutray/fd5b2efe669091f2d8b83dee94a32db3 to your computer and use it in GitHub Desktop.
// Array.from()
// The Array.from() method returns an Array object from any object with a length property or any iterable object.
// Following will return ["A", "I", "L", "J", "S", "I", "6", "4", "6", "8", "5"]
Array.from("AILJSI64685");
// Array.keys()
// The Array.keys() method returns an Array Iterator object with the keys of an array.
const fruits = ["Avocado", "Kiwi", "Apple", "Mango"];
const fruitsKeys = fruits.keys();
let keys = '';
for (let fruitKey of fruitsKeys) {
keys += fruitKey + ', ';
}
// Following will print: 0, 1, 2, 3
console.log(keys);
// Array.find()
// The find() method returns the value of the first array element that passes a test function.
// The below snippet finds the first element that is larger than 3:
const numbers = [1, 2, 3, 4, 5];
let firstElement = numbers.find((value, index, array) => value > 3);
// Following will print: 4
console.log(firstElement);
// Array.findIndex()
// The findIndex() method returns the index of the first array element that passes a test function.
// The below snippet finds the index of the first element that is larger than 3:
const numbers = [1, 2, 3, 4, 5];
let firstIndex = numbers.findIndex((value, index, array) => value > 3);
// Following will print: 3
console.log(firstIndex);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment