Skip to content

Instantly share code, notes, and snippets.

@ishar19
Created July 14, 2024 12:20
Show Gist options
  • Save ishar19/034d72774fa963a5e2f446fa8213eca2 to your computer and use it in GitHub Desktop.
Save ishar19/034d72774fa963a5e2f446fa8213eca2 to your computer and use it in GitHub Desktop.
JS method tier list
//MAP METHOD
// Define an array of numbers
const numbers = [1, 2, 3, 4, 5];
// Use the map method to create a new array with the squares of the numbers
const squares = numbers.map(number => number * number);
// Log the original array and the new array to the console
console.log("Original array:", numbers);
console.log("Squared array:", squares);
// REDUCE METHOD
const treasures = [100, 50, 200, 10];
const totalValue = treasures.reduce((total, treasure) => total + treasure, 0);
console.log(totalValue); // Outputs: 360
//FLATMAP METHOD
// Define an array of arrays with nested elements
const nestedArray = [[1, 2], [3, 4], [5, 6]];
// Use the flatMap method to create a new array with all the elements from the nested arrays
const flattenedArray = nestedArray.flatMap(subArray => subArray);
// Log the original nested array and the new flattened array to the console
console.log("Original nested array:", nestedArray);
console.log("Flattened array:", flattenedArray);
// FILTER METHOD
const books = ["A Tale of Two Cities", "The Great Gatsby", "Anna Karenina", "War and Peace"];
const aBooks = books.filter(book => book[0] === 'A');
console.log(aBooks); // Outputs: ["A Tale of Two Cities", "Anna Karenina"]
// REVERSE METHOD
const timeline = [2022, 2021, 2020, 2019, 2018];
timeline.reverse();
console.log(timeline); // Outputs: [2018, 2019, 2020, 2021, 2022]
// FILL METHOD
const canvas = new Array(10).fill('blue');
console.log(canvas); // Outputs: ["blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue", "blue"]
// AT METHOD
const suspects = ["John", "Jane", "Jim", "Joan"];
console.log(suspects.at(2)); // Outputs: "Jim"
//EVERY METHOD
const grades = [90, 85, 95, 100];
const passed = grades.every(grade => grade >= 70);
console.log(passed); // Outputs: true
// FOR EACH METHOD
const addresses = ["123 Main St", "456 Elm St", "789 Oak St"];
addresses.forEach(address => console.log(`Delivering letter to ${address}`));
// KEYS METHOD
// Define an array of fruits
const fruits = ["apple", "banana", "orange", "grape"];
// Use the keys method to get an array of the indices of the fruits
const indices = Array.from(fruits.keys());
// Log the original fruits array and the new indices array to the console
console.log("Original fruits array:", fruits);
console.log("Indices array:", indices);
//SLICE
const dough = [1, 2, 3, 4, 5];
const batch = dough.slice(1, 3);
console.log(batch); // Outputs: [2, 3]
//SPLICE
const tricks = ["poof", "shazam", "abracadabra"];
tricks.splice(1, 1, "presto");
console.log(tricks); // Outputs: ["poof", "presto", "abracadabra"]
// PUSH
const collection = ["book", "painting", "statue"];
collection.push("sculpture");
console.log(collection); // Outputs: ["book", "painting", "statue", "sculpture"]
// POP
const deck = ["Ace", "2", "3", "4", "5"];
const topCard = deck.pop();
console.log(topCard); // Outputs: "5"
console.log(deck); // Outputs: ["Ace", "2", "3", "4"]
//SHIFT
const queueShift = ["car1", "car2", "car3"];
const firstCar = queue.shift();
console.log(firstCar); // Outputs: "car1"
console.log(queue); // Outputs: ["car2", "car3"]
// UNSHIFT
const queue = ["car2", "car3"];
queue.unshift("car1");
console.log(queue); // Outputs: ["car1", "car2", "car3"]
// SORT
const booksort = ["War and Peace", "Anna Karenina", "The Great Gatsby", "A Tale of Two Cities"];
books.sort();
console.log(books); // Outputs: ["A Tale of Two Cities", "Anna Karenina", "The Great Gatsby", "War and Peace"]
// with
const arr = [1, 2, 3, 4, 5];
console.log(arr.with(2, 6)); // [1, 2, 6, 4, 5]
console.log(arr); // [1, 2, 3, 4, 5]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment