Содержание
some()
reduce()
every()
map()
flat()
filter()
forEach()
findIndex()
find()
sort()
concat()
fill()
includes()
reverse()
flatMap()
Примеры использования методов
некоторые ()
some()
true
false
const myAwesomeArray = ["a", "b", "c", "d", "e"];
console.log(myAwesomeArray.some(test => test === "d")); // true
уменьшить()
reduce()
const myAwesomeArray = [1, 2, 3, 4, 5];
console.log(myAwesomeArray.reduce((total, value) => total * value)); // 120
every()
every()
true
false
const myAwesomeArray = ["a", "b", "c", "d", "e"];
console.log(myAwesomeArray.every(test => test === "d")); // false
const myAwesomeArray2 = ["a", "a", "a", "a", "a"];
console.log(myAwesomeArray2.every(test => test === "a")); // true
map()
map()
const myAwesomeArray = [5, 4, 3, 2, 1];
console.log(myAwesomeArray.map(x => x * x)); // [25, 16, 9, 4, 1]
flat()
flat()
const myAwesomeArray = [[1, 2], [3, 4], 5];
console.log(myAwesomeArray.flat()); // [1, 2, 3, 4, 5]
фильтр()
filter()
const myAwesomeArray = [
{ id: 1, name: "john" },
{ id: 2, name: "Ali" },
{ id: 3, name: "Mass" },
{ id: 4, name: "Mass" },
];
console.log(myAwesomeArray.filter(element => element.name === "Mass"));
// [{ id: 3, name: "Mass" }, { id: 4, name: "Mass" }]
forEach()
forEach()
const myAwesomeArray = [
{ id: 1, name: "john" },
{ id: 2, name: "Ali" },
{ id: 3, name: "Mass" },
];
myAwesomeArray.forEach(element => console.log(element.name));
// john
// Ali
// Mass
findIndex()
findIndex()
const myAwesomeArray = [
{ id: 1, name: "john" },
{ id: 2, name: "Ali" },
{ id: 3, name: "Mass" },
];
console.log(myAwesomeArray.findIndex(element => element.id === 3)); // 2
console.log(myAwesomeArray.findIndex(element => element.id === 7)); // -1
найти()
find()
undefined
const myAwesomeArray = [
{ id: 1, name: "john" },
{ id: 2, name: "Ali" },
{ id: 3, name: "Mass" },
];
console.log(myAwesomeArray.find(element => element.id === 3)); // { id: 3, name: "Mass" }
console.log(myAwesomeArray.find(element => element.id === 7)); // undefined
сортировка()
sort()
const myAwesomeArray = [5, 4, 3, 2, 1];
myAwesomeArray.sort((a, b) => a - b); // [1, 2, 3, 4, 5]
myAwesomeArray.sort((a, b) => b - a); // [5, 4, 3, 2, 1]
concat()
concat()
const myAwesomeArray = [1, 2, 3, 4, 5];
const myAwesomeArray2 = [10, 20, 30, 40, 50];
console.log(myAwesomeArray.concat(myAwesomeArray2)); // [1, 2, 3, 4, 5, 10, 20, 30, 40, 50]
fill()
fill()
const myAwesomeArray = [1, 2, 3, 4, 5];
myAwesomeArray.fill(0, 1, 3); // [1, 0, 0, 4, 5]
включает в себя ()
includes()
true
false
const myAwesomeArray = [1, 2, 3, 4, 5];
console.log(myAwesomeArray.includes(3)); // true
console.log(myAwesomeArray.includes(8)); // false
reverse()
reverse()
const myAwesomeArray = ["e", "d", "c", "b", "a"];
myAwesomeArray.reverse(); // ['a', 'b', 'c', 'd', 'e']
flatMap()
flatMap()
const myAwesomeArray = [[1], [2], [3], [4], [5]];
console.log(myAwesomeArray.flatMap(arr => arr * 10)); // [10, 20, 30, 40, 50]
// With .flat() and .map()
console.log(myAwesomeArray.flat().map(arr => arr * 10)); // [10, 20, 30, 40, 50]