
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check Existence of All Continents in Array of Objects in JavaScript
Problem
We are required to write a JavaScript function that takes in an array of objects that contains data about the continent of belonging for some people.
Our function should return true if it finds six different continents in the array of objects, false otherwise.
Example
Following is the code −
const people = [ { firstName: 'Dinesh', lastName: 'A.', country: 'Algeria', continent: 'Africa', age: 25, language: 'JavaScript' }, { firstName: 'Ishan', lastName: 'M.', country: 'Chile', continent: 'South America', age: 37, language: 'C' }, { firstName: 'Rohit', lastName: 'X.', country: 'China', continent: 'Asia', age: 39, language: 'Ruby' }, { firstName: 'Manpreet', lastName: 'P.', country: 'Andorra', continent: 'Europe', age: 55, language: 'Ruby' }, { firstName: 'Rahul', lastName: 'Q.', country: 'Australia', continent: 'Australia', age: 65, language: 'PHP' }, ]; const checkAllContinent = (arr = []) => { const all = ['Africa', 'South America', 'North America', 'Europe', 'Asia', 'Australia']; const listed = arr.map(obj => { return obj.continent; }); for(let i = 0; i < listed.length; i++){ const cont = listed[i]; const ind = all.indexOf(cont); if(ind !== -1){ all.splice(ind, 1); }; }; return all.length === 0; }; console.log(checkAllContinent(people));
Output
false
Advertisements