简体   繁体   English

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

[英]Check how many elements of an array exists as keys in an object in javascript

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.我想找出数组中的每个项是否存在于对象的键中,以及在对象的键中找到了多少个数组项。

You can find the intersection of two arrays using .filter method.您可以使用.filter方法找到两个数组的交集。 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 value s that are both in daysToCheck and Obj . Object.keys(Obj)提取Obj对象的键, .filter返回一个数组,其value s 都在daysToCheckObj

Use reduce function on array and for each iteration, check if Obj has the value for the current array element.在数组上使用 reduce 函数,并在每次迭代时检查 Obj 是否具有当前数组元素的值。 If it has then increment the accumulator value by one.如果有,则将累加器值增加 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, } 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM