0

I have an object

const Obj = {
2016-07-04: 264464,
2016-07-05: 266458,
2016-07-07: 272720,
2016-07-08: 274352,
2016-07-11: 290110,
2016-07-12: 283604,
2016-07-14: 290356,
2016-07-19: 298452,
2016-07-22: 301793,
2016-07-24: 308439,
2016-07-25: 311762,
2016-07-27: 315518,
2016-07-28: 317712,
2016-07-29: 322961,
2016-07-30: 312415,
2016-07-31: 322962,
2016-08-02: 328265,
2016-08-06: 322963,
2016-08-08: 341632,
2016-08-15: 354271,
2016-08-16: 358108,
2016-08-26: 380486,
2016-08-27: 380495,
2016-08-28: 385578,
2016-08-29: 388026,
2016-08-30: 391542,
2016-09-03: 385575,
2016-09-04: 417260,
2016-09-05: 413816,
2016-09-06: 417249,
2016-09-07: 417244,
2016-09-08: 420326,
2016-09-17: 403546,
}

and I have an array

const daysToCheck = [
  "2016-09-01",
  "2016-09-02",
  "2016-09-03",
  "2016-09-04",
  "2016-09-05",
  "2016-09-06",
  "2016-09-07",
  "2016-09-08",
];

I want to find out if each of the items in the array exists in the keys of the object and how many of the items of the array are found in the keys of the object.

2
  • what does not work? Commented Oct 11, 2020 at 15:03
  • What have you tried so far? Commented Oct 11, 2020 at 15:03

2 Answers 2

1

You can find the intersection of two arrays using .filter method. Easier way of doing than what was proposed above.

let arr = Object.keys(Obj).filter( value => daysToCheck.includes(value));

Object.keys(Obj) extracts the keys of the Obj object, and the .filter return an array with values that are both in daysToCheck and Obj.

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

Comments

0

Use reduce function on array and for each iteration, check if Obj has the value for the current array element. If it has then increment the accumulator value by one.

const Obj = {
  "2016-07-04": 264464,
  "2016-07-05": 266458,
  "2016-07-07": 272720,
  "2016-07-08": 274352,
  "2016-07-11": 290110,
  "2016-07-12": 283604,
  "2016-07-14": 290356,
  "2016-07-19": 298452,
  "2016-07-22": 301793,
  "2016-07-24": 308439,
  "2016-07-25": 311762,
  "2016-07-27": 315518,
  "2016-07-28": 317712,
  "2016-07-29": 322961,
  "2016-07-30": 312415,
  "2016-07-31": 322962,
  "2016-08-02": 328265,
  "2016-08-06": 322963,
  "2016-08-08": 341632,
  "2016-08-15": 354271,
  "2016-08-16": 358108,
  "2016-08-26": 380486,
  "2016-08-27": 380495,
  "2016-08-28": 385578,
  "2016-08-29": 388026,
  "2016-08-30": 391542,
  "2016-09-03": 385575,
  "2016-09-04": 417260,
  "2016-09-05": 413816,
  "2016-09-06": 417249,
  "2016-09-07": 417244,
  "2016-09-08": 420326,
  "2016-09-17": 403546,
}
  
const daysToCheck = [
  "2016-09-01",
  "2016-09-02",
  "2016-09-03",
  "2016-09-04",
  "2016-09-05",
  "2016-09-06",
  "2016-09-07",
  "2016-09-08",
];

const keysInArray = daysToCheck.reduce((acc, cur) => {
  if (Obj[cur]) {
    acc++;
  }

  return acc;
}, 0);

console.log(keysInArray);

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.