Ланцюжки методів
const people = [
{
name: "Alex",
age: 18,
hobby: ["electronics", "programming", "sport", "gym"],
},
{
name: "Max",
age: 67,
hobby: ["reading", "cinema"],
},
{
name: "Helga",
age: 14,
hobby: ["sport", "gym", "dancing", "singing"],
},
{
name: "Mathew",
age: 36,
hobby: ["singing", "sport", "swimming"],
},
];
const youngPeople = people.filter((el) => el.age < 20);
const hobbies = youngPeople.flatMap((el) => el.hobby);
const unicHobbies = hobbies.filter((el, id, arr) => arr.indexOf(el) === id);
const sortedUnicHobbies = [...unicHobbies].sort((a, b) => a.localeCompare(b));
console.log("people: ", people); // All users
console.log("youngPeople: ", youngPeople); //Users younger than 20yo
console.log("hobbies: ", hobbies);
// ['electronics', 'programming', 'sport', 'gym', 'sport', 'gym', 'dancing', 'singing']
console.log("unicHobbies: ", unicHobbies);
// ['electronics', 'programming', 'sport', 'gym', 'dancing', 'singing']
console.log("sortedUnicHobbies: ", sortedUnicHobbies);
// ["dancing", "electronics", "gym", "programming", "singing", "sport"];

Last updated