Skip to content

Instantly share code, notes, and snippets.

@geshan

geshan/10-js-array-methods.js Secret

Created Mar 15, 2021
Embed
What would you like to do?
const programmingLanguages =[
{"id": 1,"name": "JavaScript","released_year": 1995,"githut_rank": 1,"pypl_rank": 3},
{"id": 2,"name": "Python","released_year": 1991,"githut_rank": 2,"pypl_rank": 1},
{"id": 3,"name": "Java","released_year": 1995,"githut_rank": 3,"pypl_rank": 2},
{"id": 4,"name": "TypeScript","released_year": 2012,"githut_rank": 7,"pypl_rank": 10},
{"id": 5,"name": "C#","released_year": 2000,"githut_rank": 9,"pypl_rank": 4}
];
// 1. Map
const programmingLanguageNames = programmingLanguages.map(programmingLanguage => programmingLanguage.name);
console.log(`All programming language names pulled in with map function: ${programmingLanguageNames.join()}`)
// 2. Filter
const zenZProgrammingLanguages = programmingLanguages.filter(programmingLanguage => programmingLanguage.released_year >= 2000);
console.log('Filter out the programming languages release on or after 2000: ', zenZProgrammingLanguages);
// 3. Some
const hasLanguageReleasedin1991 = programmingLanguages.some(programmingLanguage => programmingLanguage.released_year === 1991);
console.log(`Do we have any programming language in our list that was released in 1991? The answer is: ${hasLanguageReleasedin1991} using "some" function`);
// 4. Every
const allAmongTop10InGitHut = programmingLanguages.every(programmingLanguage => programmingLanguage.githut_rank <= 10);
console.log(`Are all programming languages in our list among top 10 on GitHut? The answer is: ${allAmongTop10InGitHut} using "every" function`);
// 5. Find
const mostPopularLangaugeOnPypl = programmingLanguages.find(programmingLanguage => programmingLanguage.pypl_rank === 1);
console.log(`The most popular programming language as per PYPL index is: ${mostPopularLangaugeOnPypl.name}`);
const days = ["Monday" , "Tuesday", "Wednesday", "Thursday" , "Friday", "Saturday", "Sunday"];
// 6. Includes
const isSundayADay = days.includes("Sunday");
const isFundayADay = days.includes("Funday");
console.log(`Is Sunday a day? Answer: ${isSundayADay} -- Is Funday a day? Answer: ${isFundayADay}`);
// 7. Slice
const weekendDays = days.slice(5,7);
console.log(`Generally most countries in the world get a weekend on: ${weekendDays.join()}`);
// 8. Join
const funkyDays = days.join('~');
console.log(`Days are flowing in: ${funkyDays}`);
// 9. Fill
const allSaturdays = days.fill("Saturday");
console.log(`Great! my week is a holiday week now, thanks to array fill: ${allSaturdays.join()}`);
// 10. Sort
const sortedDays = ["Monday" , "Tuesday", "Wednesday", "Thursday" , "Friday", "Saturday", "Sunday"];
sortedDays.sort();
console.log(`Days cannot be sorted but JavaScript sorts them as: ${sortedDays.join()}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment