Enea Xharja

Check if there is a missing item in an object

Given an object, we want to check if there is any missing item in it:

var footballTeam = {
  goalkeepers: {
    url: 'string',
    updatedAt: new Date(),
    createdAt: new Date(),
  },
  defenders: {
    url: 'string',
    updatedAt: new Date(),
    createdAt: new Date(),
  },
  midfielders: {
    url: 'string',
    updatedAt: new Date(),
    createdAt: new Date(),
  },
  attackers: {
    url: 'string',
    updatedAt: new Date(),
    createdAt: new Date(),
  },
};

var positions = ['goalkeepers', 'defenders', 'midfielders', 'attackers'];

var isAnyPositionMissing = positions.some(
  (position) => !footballTeam[position]
);

if (isAnyPositionMissing) {
  console.log('at least one position is missing');
} else {
  console.log("it's all good");
}

Updated on January 20, 2021

Tagged with: javascript, snippets


Profile picture

Hey there! I'm Enea, a web developer currently working at eqolot. Welcome to my little corner of the web, where I share my personal collection of notes, code snippets, and resources on topics that interest me.

© 2021 Enea Xharja