Last active
May 31, 2019 11:53
-
-
Save Kirill255/e75a3a56b21b46d797e6eaafbacfb446 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce | |
// 1 example | |
// Посчитать суммарную дистанцию | |
const trips = [ | |
{ to: "Brazil", distance: 2500 }, | |
{ to: "India", distance: 1000 }, | |
{ to: "Russia", distance: 3000 } | |
]; | |
const totalDistance = trips.reduce((acc, cur) => { | |
return acc + cur.distance; | |
}, 0); | |
console.log(totalDistance); // 6500 | |
// 2 example | |
// Нужно сделать массив из имён пользователей | |
const users = [ | |
{ name: "Donald", lastname: "Trump", age: 25 }, | |
{ name: "Jack", lastname: "Reacher", age: 31 }, | |
{ name: "Mike", lastname: "Tyson", age: 18 }, | |
{ name: "John", lastname: "Travolta", age: 22 }, | |
{ name: "Adam", lastname: "Sendler", age: 31 } | |
]; | |
const names = users.reduce((acc, cur) => { | |
acc.push(cur.name); | |
return acc; | |
}, []); | |
console.log(names); // [ 'Donald', 'Jack', 'Mike', 'John', 'Adam' ] | |
// 3 example | |
// Посчитать сколько компьютеров с каждой os | |
const computers1 = [ | |
{ type: "Laptop", price: 1000, os: "Windows" }, | |
{ type: "Desc", price: 1000, os: "Mac" }, | |
{ type: "Desc", price: 1000, os: "Windows" }, | |
{ type: "Laptop", price: 1000, os: "Windows" }, | |
{ type: "Laptop", price: 1000, os: "Mac" } | |
]; | |
const counts1 = computers1.reduce((acc, cur) => { | |
// if (cur.os === "Mac") { | |
// return { mac: acc.mac + 1, win: acc.win }; | |
// } | |
// if (cur.os === "Windows") { | |
// return { mac: acc.mac, win: acc.win + 1 }; | |
// } | |
return cur.os === "Mac" ? { mac: acc.mac + 1, win: acc.win } : { mac: acc.mac, win: acc.win + 1 }; | |
}, { mac: 0, win: 0 }); | |
console.log(counts1); // { mac: 2, win: 3 } | |
// 4 example | |
// Посчитать сколько компьютеров с каждой os (что если у нас больше наименований?) | |
const computers2 = [ | |
{ type: "Laptop", price: 1000, os: "Windows" }, | |
{ type: "Desc", price: 1000, os: "Mac" }, | |
{ type: "Desc", price: 1000, os: "Windows" }, | |
{ type: "Laptop", price: 1000, os: "Windows" }, | |
{ type: "Laptop", price: 1000, os: "Mac" }, | |
{ type: "Laptop", price: 1000, os: "Linux" }, | |
{ type: "Desc", price: 1000, os: "Windows" } | |
]; | |
const counts2 = computers2.reduce((acc, cur) => { | |
acc[cur.os] = acc[cur.os] + 1 || 1; | |
return acc; | |
}, {}); | |
console.log(counts2); // { Windows: 4, Mac: 2, Linux: 1 } | |
// 5 example | |
// На входе массив, на выходе объект | |
const arr = [ | |
{ name: "width", value: 10 }, | |
{ name: "height", value: 20 } | |
]; | |
const obj = arr.reduce((acc, {name, value}) => acc[name] = value, {}); | |
console.log(obj); // {width: 10, height: 20} | |
// 6 example | |
// Выведите чётные и нечётные символы в представленной строке, ввод "Hacker" -> вывод "Hce akr", ввод "Rank" -> вывод "Rn ak" | |
const word = "Hacker" | |
word.split("") | |
.reduce(([evens, odds], current, i) => (i % 2 === 0 ? [evens + current, odds] : [evens, odds + current]), ["", ""]) | |
.join(" "); | |
// or | |
word.split("") | |
.reduce((arr, char, i) => (arr[i % 2] += char) && arr, ["", ""]) | |
.join(" "); | |
console.log(word); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment