Created
November 11, 2017 21:38
-
-
Save j3ck/c45d83e06a29efe25327c575c56e648c 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
| /* | |
| * #1 | |
| * Получить из данного массива пользователей | |
| * массив их полных имен | |
| * EX: ['Vasya Vasiliev', 'Ivan Ivanov'] | |
| */ | |
| { | |
| const users = [ | |
| { id: 1, name: 'Vasya', surname: 'Vasiliev' }, | |
| { id: 2, name: 'Ivan', surname: 'Ivanov' }, | |
| { id: 3, name: 'Irina', surname: 'Plushkina' } | |
| ]; | |
| full_names = (arr) => { | |
| return arr.map((el) => { return `${el.name} ${el.surname}` }); | |
| } | |
| console.log(full_names(users)); | |
| } | |
| /* | |
| * #2 | |
| * Получить из данного массива пользователей | |
| * только тех, кто старше определенного возраста | |
| */ | |
| { | |
| const users = [ | |
| { id: 1, age: 21, name: 'Vasya', surname: 'Vasiliev' }, | |
| { id: 2, age: 28, name: 'Ivan', surname: 'Ivanov' }, | |
| { id: 3, age: 18, name: 'Irina', surname: 'Plushkina' } | |
| ]; | |
| older = (age, arr) => { | |
| return arr.filter((el) => { return el.age > age }); | |
| } | |
| console.log(older(20, users)) | |
| } | |
| /* | |
| * #3 | |
| * Получить из данного массива заказов | |
| * среднюю сумму заказа | |
| */ | |
| { | |
| const orders = [{ | |
| id: 5, | |
| date: '21-01-2015', | |
| amount: 783 | |
| }, { | |
| id: 8, | |
| date: '24-01-2015', | |
| amount: 67 | |
| }, { | |
| id: 21, | |
| date: '29-01-2015', | |
| amount: 1234 | |
| }, { | |
| id: 78, | |
| date: '04-02-2015', | |
| amount: 123 | |
| }, { | |
| id: 23, | |
| date: '15-02-2015', | |
| amount: 245 | |
| }]; | |
| avg_amount = (arr) => { | |
| const sum = arr.reduce((previousValue, currentValue) => { | |
| return previousValue + currentValue.amount | |
| }, 0); | |
| return sum / arr.length | |
| } | |
| console.log(avg_amount(orders)) | |
| } | |
| /* | |
| * #4 | |
| * Получить из данного массива пользователей | |
| * распределение по интересам | |
| * EX: { computers: 3, food: 5, math: 1, cats: 3 } | |
| * где ключ соответствует названию интреса | |
| * а значение - количество людей с таким интересом | |
| */ | |
| { | |
| const users = [{ | |
| name: 'Vasya', | |
| surname: 'Ivanov', | |
| interests: ['computers', 'food'] | |
| }, { | |
| name: 'Ivan', | |
| surname: 'Tretyakov', | |
| interests: ['computers', 'food', 'cars'] | |
| }, { | |
| name: 'Daryna', | |
| surname: 'Petrova', | |
| interests: ['cars', 'math'] | |
| }, { | |
| name: 'Petro', | |
| surname: 'Nalyvaiko', | |
| interests: ['computers', 'food', 'math'] | |
| }]; | |
| interests = (arr) => { | |
| return users.reduce((previousValue, currentValue) => { | |
| currentValue.interests.forEach((el) => { | |
| isNaN(previousValue[el]) ? previousValue[el] = 1 : previousValue[el] += 1 | |
| }); | |
| return previousValue | |
| }, {}); | |
| } | |
| console.log(interests(users)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment