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 −

 Live Demo

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
Updated on: 2021-04-17T13:19:26+05:30

124 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements