Last active
January 22, 2021 16:49
-
-
Save asimonok/e78292accec046c1b70f1a107507ee96 to your computer and use it in GitHub Desktop.
hello
This file contains 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
const names = ['Alex', 'John', 'Marie']; | |
const ages = [20, 30, 25]; | |
const person = { | |
name: '', | |
age: 0, | |
} | |
const persons = []; | |
names.forEach((name, index) => { | |
persons.push({ | |
name, | |
age: ages[index], | |
}) | |
}); | |
const personsKeys = persons | |
.map(({ name, age }) => `${name}:${age}`) | |
const agerageAge = ages.reduce((acc, age, index, array) => { | |
return acc + age / array.length; | |
}, 0) | |
const personsMap = persons.reduce((acc, person) => { | |
return { | |
...acc, | |
[person.name]: person, | |
} | |
}, {}) | |
Object.entries(personsMap).forEach(([key, value]) => { | |
console.log(key, value); | |
}) | |
Object.keys(personsMap).forEach((key) => { | |
console.log(key, personsMap[key]); | |
}) | |
console.log('includes', names.includes('Alex')) | |
console.log('some', persons.some((item) => item.name === 'Alex')) | |
console.log('every', persons.every((item) => item.age > 18)) | |
console.log('find', persons.find((item) => item.name === 'Alex')) | |
console.log('findIndex', persons.findIndex((item) => item.name === 'Alex')) | |
console.log('filter', persons.filter((item) => item.age > 21)) | |
// mutate | |
const array = []; | |
array.push() | |
array.sort() | |
array.revert() | |
array.splice() | |
array.pop() | |
... | |
const customSort = (persons) => { | |
//... | |
return [...persons].sort(); | |
} | |
const result = customSort(persons); | |
const average = persons.reduce(); | |
console.log(persons); | |
console.log(agerageAge) | |
console.log(personsMap) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment