You need to select at least one option.
None of the methods provided here mutate the original array.
These methods aren't included here:
Array.prototype.flat()
Array.prototype.flatMap()
Array.prototype.reduceRight()
Array.prototype.slice()
Array.prototype.toString()
Array.prototype.map()
If you're looking for another array with all elements of the original array being modified by some function, then you should use the .map()
method.
// pass a function to map
const result = ['Cat', 'Griffin', 'Wolf'].map(x => 'School of ' + x);
console.log(result);
// Output: Array ['School of Cat', 'School of Griffin', 'School of Wolf']
Array.prototype.filter()
If you're looking for all elements which match a condition, then you should use the .filter()
method.
// pass a function to filter
const words = ['witcher', 'hunter', 'king', 'guard'];
const allMatches = words.filter(x => x.length < 6);
console.log(allMatches);
// Output: Array ['king', 'guard']
Array.prototype.concat()
If you're looking to merge multiple arrays without changing the original arrays, then you should use the .concat()
method.
const heroes = ['Geralt', 'Vesemir'].concat(['Ciri', 'Yennefer']);
console.log(heroes);
// Output: Array ['Geralt', 'Vesemir', 'Ciri', 'Yennefer']
Array.prototype.find()
If you're looking for the first element which matches a condition from a given array, then you should use the .find()
method.
// pass a function to find
const words = ['witcher', 'hunter', 'king', 'guard'];
const firstMatch = words.find(x => x.length < 6);
console.log(firstMatch);
// Output: String 'king'
Array.prototype.findIndex()
If you're looking for the index of the first element which matches a condition from a given array, then you should use the .findIndex()
method.
// pass a function to findIndex
const words = ['witcher', 'hunter', 'king', 'guard'];
const firstMatch = words.findIndex(x => x.length < 6);
console.log(firstMatch);
// Output: 2
Array.prototype.join()
If you're looking to combine all array elements (with something separating them or not), then you should use the .join()
method.
const result = ['Toss', 'a', 'coin'].join(' '));
console.log(result);
// Output: String 'Toss a coin'
Array.prototype.every()
If you're checking whether every element from a given array matches a certain condition, then you should use the .every()
method.
// pass a function to every
const words = ['witcher', 'hunter', 'king', 'guard'];
const lessThan10 = words.every(x => x.length < 10);
console.log(lessThan10);
// Output: true
const greaterThan10 = words.every(x => x.length > 10);
console.log(greaterThan10);
// Output: false
Array.prototype.some()
If you're checking whether at least one element from a given array matches a certain condition, then you should use the .some()
method.
// pass a function to some
const words = ['witcher', 'hunter', 'king', 'guard'];
const anyLessThan6 = words.some(x => x.length < 6);
console.log(anyLessThan6);
// Output: true
const anyGreaterThan10 = words.some(x => x.length > 10);
console.log(anyGreaterThan10);
// Output: false
Array.prototype.includes()
If you're checking whether a certain element exists in the given array, then you should use the .includes()
method.
const witchers = ['Geralt', 'Ciri', 'Vesemir'];
const isCiriAWitcher = witchers.includes('Ciri');
console.log(isCiriAWitcher);
// Output: true
const isYenneferAWitcher = words.includes('Yennefer');
console.log(isYenneferAWitcher);
// Output: false
Array.prototype.forEach()
If you're looking to perform some operation(s) on every element from a given array, then you should use the .forEach()
method.
['Geralt', 'Vesemir', 'Ciri'].forEach(x => {
console.log(x + ', the Witcher');
});
// Output:
// Geralt, the Witcher
// Vesemi, the Witcher
// Ciri, the Witcher
Array.prototype.reduce()
If you're looking to calculate a single value from array elements, then you should use the .reduce()
method.
// This example is for numbers, but it works for strings as well.
const numbers = [1, 2, 3, 4];
const reducerFunction = (inventory, loot) => inventory + loot;
const saddleBag = numbers.reduce(reducerFunction);
console.log(saddleBag); // Output: 10
const existingLoot = 50;
const stash = numbers.reduce(reducerFunction, existingLoot);
console.log(stash); // Output: 60