0

How can I check if the items in an array like:

var dayPartsArr = ['PT','WDM','WDD','EF','WKE'];

are also the keys in an object like:

 var dayPartsMap = {
   'PT': 'PRIME TIME',
   'WDM': 'WEEKDAY MORNING',
   'WDD': 'WEEKDAY DAYTIME',
   'EF': 'EARLY FRINGE',
   'WKE': 'WEEKEND DAYTIME'
 };

Keep in mind the order of the dayPartsArr should not matter. So if

var dayPartsArr = ['WKE','PT','WDM','WDD','EF'];

I still want to be able to use dayPartsMap to check if each item in dayPartsArr is a key in dayPartsMap

1
  • Why not just use a proper Map? Commented Jul 11, 2018 at 1:39

3 Answers 3

2

Ignoring Order

Ignoring order, we can take the input array and compare it to the keys like so:

var dayPartsArr = ['WDM','PT','WDD','EF','WKE']; //out of order

var dayPartsMap = {
  'PT': 'PRIME TIME',
  'WDM': 'WEEKDAY MORNING',
  'WDD': 'WEEKDAY DAYTIME',
  'EF': 'EARLY FRINGE',
  'WKE': 'WEEKEND DAYTIME'
};

dayPartsArr.every(item => ~Object.keys(dayPartsMap).indexOf(item)); // true

// Compare against an array with a missing key
dayPartsArr = ['WDM','PT','FAKE', 'WDD','EF','WKE'];

dayPartsArr.every(item => ~Object.keys(dayPartsMap).indexOf(item)); // false

Note the ~ is the bitwise NOT operator, it helps with indexOf

Previous Answer

You can convert the Object's keys to an array and compare that array, like so:

For more information on Object.keys, check MDN here.

var dayPartsArr = ['PT','WDM','WDD','EF','WKE'];

var dayPartsMap = {
  'PT': 'PRIME TIME',
  'WDM': 'WEEKDAY MORNING',
  'WDD': 'WEEKDAY DAYTIME',
  'EF': 'EARLY FRINGE',
  'WKE': 'WEEKEND DAYTIME'
};

// Create the comparison function...
function arraysIdentical(a, b) {
   var i = a.length;
   if (i != b.length) return false;
   while (i--) {
       if (a[i] !== b[i]) return false;
   }
       return true;
};

/// compare!
arraysIdentical(dayPartsArr, Object.keys(dayPartsMap)); // true

arraysIdentical originally suggested by Tim Down

Sign up to request clarification or add additional context in comments.

2 Comments

What about if the items in dayPartsArr were in a different order? would this still work? See my revised question above
@NewToJS Note this also works if the dayPartsArr contains duplicates.
0

We can use every and filter to solve the issue.

Ref:

var dayPartsArr = ['PT','WDM','WDD','EF','WKE'];
var dayPartsMap = {
   'PT': 'PRIME TIME',
   'WDM': 'WEEKDAY MORNING',
   'WDD': 'WEEKDAY DAYTIME',
   'EF': 'EARLY FRINGE',
   'WKE': 'WEEKEND DAYTIME', 
};

// > 1 for WKE
// var dayPartsArr = ['PT','WDM','WDD','EF','WKE', 'WKE'];

const check = Object.keys(dayPartsMap).every(key =>   dayPartsArr.filter(day => day === key).length === 1);

console.log(check);

Comments

0

You can use Array.prototype.every to check if all items of dayPartsArr are present in dayPartsMap keys:

var dayPartsArr = ['PT','WDM','WDD','EF','WKE'];

var dayPartsMap = { 'PT': 'PRIME TIME', 'WDM': 'WEEKDAY MORNING', 'WDD': 'WEEKDAY DAYTIME', 'EF': 'EARLY FRINGE', 'WKE': 'WEEKEND DAYTIME' };

var objKeys = Object.keys(dayPartsMap);
var result = dayPartsArr.every(day => objKeys.includes(day));

console.log(result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.