Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/475.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查javascript中一个对象中有多少数组元素作为键存在_Javascript_Arrays_Object - Fatal编程技术网

检查javascript中一个对象中有多少数组元素作为键存在

检查javascript中一个对象中有多少数组元素作为键存在,javascript,arrays,object,Javascript,Arrays,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-2

我有一个目标

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",
];

我想知道数组中的每个项是否都存在于对象的键中,以及在对象的键中找到了数组中的多少项。

对数组使用reduce函数,对于每个迭代,检查Obj是否具有当前数组元素的值。如果有,则将累加器值增加1

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,
}
常数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++;
}
返回acc;
}, 0);

console.log(keysInArray)
您可以使用
.filter
方法找到两个数组的交点。比上述建议更简单的方法

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

Object.keys(Obj)
提取
Obj
对象的键,然后
.filter
返回一个带有
值的数组,这些值都在
日期检查
Obj

什么不起作用?到目前为止您尝试了什么?