比较两个数组并使用javascript查找每个值计数

时间:2017-08-01 07:51:19

标签: javascript arrays

我有两个阵列。

这是我的阵列。

 var arr=[
"08-01-2017",
"07-31-2017",
"07-30-2017",
"07-29-2017",
"07-28-2017",
"07-27-2017",
"07-26-2017"]

这是另一个数组

 var arr1= [
      {
        "HOST": "",
        "U_ID": "",
        "PATH": "Home",
        "REFERER": "",
        "AGENT": "",
        "OS": "",
        "DATE_TIME": "2017-07-31T05:44:06.743Z",
        "S_ID": "80067826-0cad-4806-b40e-8e5d8be18e53",
        "C_ID": "",
        "C_NAME": "",
        "P_ID": "",
        "P_NAME": "",
        "CART_ID": "",
        "O_ID": "",
        "id": "597c20a636c81d38dc77c626"
      },
      {
        "HOST": "",
        "U_ID": "",
        "PATH": "ProductlistPage",
        "REFERER": "Home",
        "AGENT": "",
        "OS": "",
        "DATE_TIME": "2017-07-30T05:44:22.425Z",
        "S_ID": "7fe1795a-5064-43d7-9a62-48b0f156a2c3",
        "C_ID": "1",
        "C_NAME": "Sparklers",
        "P_ID": "",
        "P_NAME": "",
        "CART_ID": "",
        "O_ID": "",
        "id": "597c20b636c81d38dc77c627"
      },
      {
        "HOST": "",
        "U_ID": "",
        "PATH": "ProductlistPage",
        "REFERER": "Home",
        "AGENT": "",
        "OS": "",
        "DATE_TIME": "2017-07-29T05:45:25.064Z",
        "S_ID": "439667fe-ab24-4bf6-959e-152a4f631fe3",
        "C_ID": "595f40038c4d19b50ac21b98",
        "C_NAME": "Sparklers",
        "P_ID": "",
        "P_NAME": "",
        "CART_ID": "",
        "O_ID": "",
        "id": "597c20f536c81d38dc77c628"
      },
      {
        "HOST": "",
        "U_ID": "",
        "PATH": "ProductdetailsPage",
        "REFERER": "ProductlistPage",
        "AGENT": "",
        "OS": "",
        "DATE_TIME": "2017-07-29T05:45:39.047Z",
        "S_ID": "a21c273a-372e-4a81-9997-d468d5efd299",
        "C_ID": "1",
        "C_NAME": "Sparklers",
        "P_ID": "595f3e418c4d19b50ac21b84",
        "P_NAME": "Martina",
        "CART_ID": "",
        "O_ID": "",
        "id": "597c210236c81d38dc77c629"
      },
      {
        "HOST": "",
        "U_ID": "",
        "PATH": "Home",
        "REFERER": "",
        "AGENT": "",
        "OS": "",
        "DATE_TIME": "2017-07-28T05:47:15.604Z",
        "S_ID": "a4278659-8053-45fd-8a95-78766fd7541b",
        "C_ID": "",
        "C_NAME": "",
        "P_ID": "",
        "P_NAME": "",
        "CART_ID": "",
        "O_ID": "",
        "id": "597c216336c81d38dc77c62a"
      },
      {
        "HOST": "",
        "U_ID": "",
        "PATH": "ProductlistPage",
        "REFERER": "Home",
        "AGENT": "",
        "OS": "",
        "DATE_TIME": "2017-07-27T05:47:22.700Z",
        "S_ID": "874bb326-ab02-4061-a029-bef5977d4c65",
        "C_ID": "595f40038c4d19b50ac21b98",
        "C_NAME": "Sparklers",
        "P_ID": "",
        "P_NAME": "",
        "CART_ID": "",
        "O_ID": "",
        "id": "597c216b36c81d38dc77c62b"
      },
      {
        "HOST": "",
        "U_ID": "",
        "PATH": "CartPage",
        "REFERER": "ProductlistPage",
        "AGENT": "",
        "OS": "",
        "DATE_TIME": "2017-07-27T05:47:25.758Z",
        "S_ID": "dd9096f9-79ea-4d86-81ce-149e364ab1ed",
        "C_ID": "1",
        "C_NAME": "Sparklers",
        "P_ID": "",
        "P_NAME": "",
        "CART_ID": "",
        "O_ID": "",
        "id": "597c216d36c81d38dc77c62c"
      },
      {
        "HOST": "",
        "U_ID": "",
        "PATH": "ProductdetailsPage",
        "REFERER": "ProductlistPage",
        "AGENT": "",
        "OS": "",
        "DATE_TIME": "2017-07-29T05:47:42.047Z",
        "S_ID": "4ed84c84-80ec-4494-9ffe-453f19e2c079",
        "C_ID": "1",
        "P_ID": "595f3e418c4d19b50ac21b84",
        "P_NAME": "Martina",
        "CART_ID": "",
        "O_ID": "",
        "id": "597c217d36c81d38dc77c62d"
      },
      {
        "HOST": "",
        "U_ID": "",
        "PATH": "CartPage",
        "REFERER": "ProductdetailsPage",
        "AGENT": "",
        "OS": "",
        "DATE_TIME": "2017-07-29T05:47:45.174Z",
        "S_ID": "91153a97-d79e-4d86-9872-5df51bd7e82f",
        "C_ID": "1",
        "P_ID": "595f3e418c4d19b50ac21b84",
        "P_NAME": "Martina",
        "CART_ID": "",
        "O_ID": "",
        "id": "597c218136c81d38dc77c62e"
      },
      {
        "HOST": "",
        "U_ID": "59687ee6cd62a604e2bb0d1f",
        "PATH": "CustomershippingPage",
        "REFERER": "cartPage",
        "AGENT": "",
        "OS": "",
        "DATE_TIME": "2017-07-29T05:48:20.386Z",
        "S_ID": "46866f41-9f43-4d28-a602-490db68dc029",
        "C_ID": "",
        "C_NAME": "",
        "P_ID": "",
        "P_NAME": "",
        "CART_ID": "597c21a336c81d38dc77c62f",
        "O_ID": "",
        "id": "597c21a436c81d38dc77c630"
      },
      {
        "HOST": "",
        "U_ID": "59687ee6cd62a604e2bb0d1f",
        "PATH": "customerpayment",
        "REFERER": "CustomershippingPage",
        "AGENT": "",
        "OS": "",
        "DATE_TIME": "2017-01-08T05:48:46.356Z",
        "S_ID": "56ad3bd7-854b-42e9-8443-46c514eb2ece",
        "C_ID": "",
        "C_NAME": "",
        "P_ID": "",
        "P_NAME": "",
        "CART_ID": "597c21a336c81d38dc77c62f",
        "O_ID": "",
        "id": "597c21be36c81d38dc77c631"
       }
    ]

我需要比较两个arrays.find匹配值并且还要计数。

示例arr是date的列表。根据arr1比较另一个数组DATE_TIME

08-01-2017重复一次。并且07-29-2017重复了五次。

我需要这个输出

 08-01-2017 - 2 <- Count(How many times repeated)
 07-29-2017 - 5
 07-28-2017 - 3 etc..

每个(arr)值是重复的次数。

我不知道。

请建议我,

谢谢

4 个答案:

答案 0 :(得分:2)

您可以将日期标准化为ISO 6801字符串,因为DATE_TIME的时间戳就像。然后在一个以日期为关键字的对象中只添加一个。

var data = [{ DATE_TIME: "2017-07-31T05:44:06.743Z" }, { DATE_TIME: "2017-07-30T05:44:22.425Z" }, { DATE_TIME: "2017-07-29T05:45:25.064Z" }, { DATE_TIME: "2017-07-29T05:45:39.047Z" }, { DATE_TIME: "2017-07-28T05:47:15.604Z" }, { DATE_TIME: "2017-07-27T05:47:22.700Z" }, { DATE_TIME: "2017-07-27T05:47:25.758Z" }, { DATE_TIME: "2017-07-29T05:47:42.047Z" }, { DATE_TIME: "2017-07-29T05:47:45.174Z" }, { DATE_TIME: "2017-07-29T05:48:20.386Z" }, { DATE_TIME: "2017-01-08T05:48:46.356Z" }],
    dates = ["08-01-2017", "07-31-2017", "07-30-2017", "07-29-2017", "07-28-2017", "07-27-2017", "07-26-2017"],
    count = Object.create(null);

dates.forEach(function (d) {
    count[d.replace(/(..)-(..)-(....)/, '$3-$1-$2')] = 0;
});
data.forEach(function (o) {
    var key = o.DATE_TIME.slice(0, 10);
    (key in count) && count[key]++;
});
   
console.log(count);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:1)

您最初可以迭代arr并将结果中的所有日期作为值0的键放置。然后您可以迭代arr1并使用每个日期作为结果对象的键并继续递增计算每个日期。 像这样的东西:

&#13;
&#13;
var arr=[
"08-01-2017",
"07-31-2017",
"07-30-2017",
"07-29-2017",
"07-28-2017",
"07-27-2017",
"07-26-2017"];

var arr1= [
  {
    "HOST": "",
    "U_ID": "",
    "PATH": "Home",
    "REFERER": "",
    "AGENT": "",
    "OS": "",
    "DATE_TIME": "2017-07-31T05:44:06.743Z",
    "S_ID": "80067826-0cad-4806-b40e-8e5d8be18e53",
    "C_ID": "",
    "C_NAME": "",
    "P_ID": "",
    "P_NAME": "",
    "CART_ID": "",
    "O_ID": "",
    "id": "597c20a636c81d38dc77c626"
  },
  {
    "HOST": "",
    "U_ID": "",
    "PATH": "ProductlistPage",
    "REFERER": "Home",
    "AGENT": "",
    "OS": "",
    "DATE_TIME": "2017-07-30T05:44:22.425Z",
    "S_ID": "7fe1795a-5064-43d7-9a62-48b0f156a2c3",
    "C_ID": "1",
    "C_NAME": "Sparklers",
    "P_ID": "",
    "P_NAME": "",
    "CART_ID": "",
    "O_ID": "",
    "id": "597c20b636c81d38dc77c627"
  },
  {
    "HOST": "",
    "U_ID": "",
    "PATH": "ProductlistPage",
    "REFERER": "Home",
    "AGENT": "",
    "OS": "",
    "DATE_TIME": "2017-07-29T05:45:25.064Z",
    "S_ID": "439667fe-ab24-4bf6-959e-152a4f631fe3",
    "C_ID": "595f40038c4d19b50ac21b98",
    "C_NAME": "Sparklers",
    "P_ID": "",
    "P_NAME": "",
    "CART_ID": "",
    "O_ID": "",
    "id": "597c20f536c81d38dc77c628"
  },
  {
    "HOST": "",
    "U_ID": "",
    "PATH": "ProductdetailsPage",
    "REFERER": "ProductlistPage",
    "AGENT": "",
    "OS": "",
    "DATE_TIME": "2017-07-29T05:45:39.047Z",
    "S_ID": "a21c273a-372e-4a81-9997-d468d5efd299",
    "C_ID": "1",
    "C_NAME": "Sparklers",
    "P_ID": "595f3e418c4d19b50ac21b84",
    "P_NAME": "Martina",
    "CART_ID": "",
    "O_ID": "",
    "id": "597c210236c81d38dc77c629"
  },
  {
    "HOST": "",
    "U_ID": "",
    "PATH": "Home",
    "REFERER": "",
    "AGENT": "",
    "OS": "",
    "DATE_TIME": "2017-07-28T05:47:15.604Z",
    "S_ID": "a4278659-8053-45fd-8a95-78766fd7541b",
    "C_ID": "",
    "C_NAME": "",
    "P_ID": "",
    "P_NAME": "",
    "CART_ID": "",
    "O_ID": "",
    "id": "597c216336c81d38dc77c62a"
  },
  {
    "HOST": "",
    "U_ID": "",
    "PATH": "ProductlistPage",
    "REFERER": "Home",
    "AGENT": "",
    "OS": "",
    "DATE_TIME": "2017-07-27T05:47:22.700Z",
    "S_ID": "874bb326-ab02-4061-a029-bef5977d4c65",
    "C_ID": "595f40038c4d19b50ac21b98",
    "C_NAME": "Sparklers",
    "P_ID": "",
    "P_NAME": "",
    "CART_ID": "",
    "O_ID": "",
    "id": "597c216b36c81d38dc77c62b"
  },
  {
    "HOST": "",
    "U_ID": "",
    "PATH": "CartPage",
    "REFERER": "ProductlistPage",
    "AGENT": "",
    "OS": "",
    "DATE_TIME": "2017-07-27T05:47:25.758Z",
    "S_ID": "dd9096f9-79ea-4d86-81ce-149e364ab1ed",
    "C_ID": "1",
    "C_NAME": "Sparklers",
    "P_ID": "",
    "P_NAME": "",
    "CART_ID": "",
    "O_ID": "",
    "id": "597c216d36c81d38dc77c62c"
  },
  {
    "HOST": "",
    "U_ID": "",
    "PATH": "ProductdetailsPage",
    "REFERER": "ProductlistPage",
    "AGENT": "",
    "OS": "",
    "DATE_TIME": "2017-07-29T05:47:42.047Z",
    "S_ID": "4ed84c84-80ec-4494-9ffe-453f19e2c079",
    "C_ID": "1",
    "P_ID": "595f3e418c4d19b50ac21b84",
    "P_NAME": "Martina",
    "CART_ID": "",
    "O_ID": "",
    "id": "597c217d36c81d38dc77c62d"
  },
  {
    "HOST": "",
    "U_ID": "",
    "PATH": "CartPage",
    "REFERER": "ProductdetailsPage",
    "AGENT": "",
    "OS": "",
    "DATE_TIME": "2017-07-29T05:47:45.174Z",
    "S_ID": "91153a97-d79e-4d86-9872-5df51bd7e82f",
    "C_ID": "1",
    "P_ID": "595f3e418c4d19b50ac21b84",
    "P_NAME": "Martina",
    "CART_ID": "",
    "O_ID": "",
    "id": "597c218136c81d38dc77c62e"
  },
  {
    "HOST": "",
    "U_ID": "59687ee6cd62a604e2bb0d1f",
    "PATH": "CustomershippingPage",
    "REFERER": "cartPage",
    "AGENT": "",
    "OS": "",
    "DATE_TIME": "2017-07-29T05:48:20.386Z",
    "S_ID": "46866f41-9f43-4d28-a602-490db68dc029",
    "C_ID": "",
    "C_NAME": "",
    "P_ID": "",
    "P_NAME": "",
    "CART_ID": "597c21a336c81d38dc77c62f",
    "O_ID": "",
    "id": "597c21a436c81d38dc77c630"
  },
  {
    "HOST": "",
    "U_ID": "59687ee6cd62a604e2bb0d1f",
    "PATH": "customerpayment",
    "REFERER": "CustomershippingPage",
    "AGENT": "",
    "OS": "",
    "DATE_TIME": "2017-01-08T05:48:46.356Z",
    "S_ID": "56ad3bd7-854b-42e9-8443-46c514eb2ece",
    "C_ID": "",
    "C_NAME": "",
    "P_ID": "",
    "P_NAME": "",
    "CART_ID": "597c21a336c81d38dc77c62f",
    "O_ID": "",
    "id": "597c21be36c81d38dc77c631"
   }
];

var result = {};


arr.forEach(function(date){
   result[date] = 0;
})


arr1.forEach(function(ob){
    var date = new Date(ob['DATE_TIME']);
    var m = date.getMonth() + 1;
    m = m > 9 ? m : '0' + m;
    var d = date.getDate() + 1;
    d = d > 9 ? d : '0' + d;
    date = m + '-' + d + '-' +  date.getFullYear();
    result[date] = date in result ? parseInt(result[date]) + 1 : 1;
})


console.log(result);
&#13;
&#13;
&#13;

答案 2 :(得分:1)

您可以从ISO日期中提取日期,然后使用用户数组#reduce和array#filter来计算出现次数。

const arr=["08-01-2017","07-31-2017","07-30-2017","07-29-2017","07-28-2017","07-27-2017","07-26-2017"];
const arr1= [
      {
        "HOST": "",
        "U_ID": "",
        "PATH": "Home",
        "REFERER": "",
        "AGENT": "",
        "OS": "",
        "DATE_TIME": "2017-07-31T05:44:06.743Z",
        "S_ID": "80067826-0cad-4806-b40e-8e5d8be18e53",
        "C_ID": "",
        "C_NAME": "",
        "P_ID": "",
        "P_NAME": "",
        "CART_ID": "",
        "O_ID": "",
        "id": "597c20a636c81d38dc77c626"
      },
      {
        "HOST": "",
        "U_ID": "",
        "PATH": "ProductlistPage",
        "REFERER": "Home",
        "AGENT": "",
        "OS": "",
        "DATE_TIME": "2017-07-30T05:44:22.425Z",
        "S_ID": "7fe1795a-5064-43d7-9a62-48b0f156a2c3",
        "C_ID": "1",
        "C_NAME": "Sparklers",
        "P_ID": "",
        "P_NAME": "",
        "CART_ID": "",
        "O_ID": "",
        "id": "597c20b636c81d38dc77c627"
      },
      {
        "HOST": "",
        "U_ID": "",
        "PATH": "ProductlistPage",
        "REFERER": "Home",
        "AGENT": "",
        "OS": "",
        "DATE_TIME": "2017-07-29T05:45:25.064Z",
        "S_ID": "439667fe-ab24-4bf6-959e-152a4f631fe3",
        "C_ID": "595f40038c4d19b50ac21b98",
        "C_NAME": "Sparklers",
        "P_ID": "",
        "P_NAME": "",
        "CART_ID": "",
        "O_ID": "",
        "id": "597c20f536c81d38dc77c628"
      },
      {
        "HOST": "",
        "U_ID": "",
        "PATH": "ProductdetailsPage",
        "REFERER": "ProductlistPage",
        "AGENT": "",
        "OS": "",
        "DATE_TIME": "2017-07-29T05:45:39.047Z",
        "S_ID": "a21c273a-372e-4a81-9997-d468d5efd299",
        "C_ID": "1",
        "C_NAME": "Sparklers",
        "P_ID": "595f3e418c4d19b50ac21b84",
        "P_NAME": "Martina",
        "CART_ID": "",
        "O_ID": "",
        "id": "597c210236c81d38dc77c629"
      },
      {
        "HOST": "",
        "U_ID": "",
        "PATH": "Home",
        "REFERER": "",
        "AGENT": "",
        "OS": "",
        "DATE_TIME": "2017-07-28T05:47:15.604Z",
        "S_ID": "a4278659-8053-45fd-8a95-78766fd7541b",
        "C_ID": "",
        "C_NAME": "",
        "P_ID": "",
        "P_NAME": "",
        "CART_ID": "",
        "O_ID": "",
        "id": "597c216336c81d38dc77c62a"
      },
      {
        "HOST": "",
        "U_ID": "",
        "PATH": "ProductlistPage",
        "REFERER": "Home",
        "AGENT": "",
        "OS": "",
        "DATE_TIME": "2017-07-27T05:47:22.700Z",
        "S_ID": "874bb326-ab02-4061-a029-bef5977d4c65",
        "C_ID": "595f40038c4d19b50ac21b98",
        "C_NAME": "Sparklers",
        "P_ID": "",
        "P_NAME": "",
        "CART_ID": "",
        "O_ID": "",
        "id": "597c216b36c81d38dc77c62b"
      },
      {
        "HOST": "",
        "U_ID": "",
        "PATH": "CartPage",
        "REFERER": "ProductlistPage",
        "AGENT": "",
        "OS": "",
        "DATE_TIME": "2017-07-27T05:47:25.758Z",
        "S_ID": "dd9096f9-79ea-4d86-81ce-149e364ab1ed",
        "C_ID": "1",
        "C_NAME": "Sparklers",
        "P_ID": "",
        "P_NAME": "",
        "CART_ID": "",
        "O_ID": "",
        "id": "597c216d36c81d38dc77c62c"
      },
      {
        "HOST": "",
        "U_ID": "",
        "PATH": "ProductdetailsPage",
        "REFERER": "ProductlistPage",
        "AGENT": "",
        "OS": "",
        "DATE_TIME": "2017-07-29T05:47:42.047Z",
        "S_ID": "4ed84c84-80ec-4494-9ffe-453f19e2c079",
        "C_ID": "1",
        "P_ID": "595f3e418c4d19b50ac21b84",
        "P_NAME": "Martina",
        "CART_ID": "",
        "O_ID": "",
        "id": "597c217d36c81d38dc77c62d"
      },
      {
        "HOST": "",
        "U_ID": "",
        "PATH": "CartPage",
        "REFERER": "ProductdetailsPage",
        "AGENT": "",
        "OS": "",
        "DATE_TIME": "2017-07-29T05:47:45.174Z",
        "S_ID": "91153a97-d79e-4d86-9872-5df51bd7e82f",
        "C_ID": "1",
        "P_ID": "595f3e418c4d19b50ac21b84",
        "P_NAME": "Martina",
        "CART_ID": "",
        "O_ID": "",
        "id": "597c218136c81d38dc77c62e"
      },
      {
        "HOST": "",
        "U_ID": "59687ee6cd62a604e2bb0d1f",
        "PATH": "CustomershippingPage",
        "REFERER": "cartPage",
        "AGENT": "",
        "OS": "",
        "DATE_TIME": "2017-07-29T05:48:20.386Z",
        "S_ID": "46866f41-9f43-4d28-a602-490db68dc029",
        "C_ID": "",
        "C_NAME": "",
        "P_ID": "",
        "P_NAME": "",
        "CART_ID": "597c21a336c81d38dc77c62f",
        "O_ID": "",
        "id": "597c21a436c81d38dc77c630"
      },
      {
        "HOST": "",
        "U_ID": "59687ee6cd62a604e2bb0d1f",
        "PATH": "customerpayment",
        "REFERER": "CustomershippingPage",
        "AGENT": "",
        "OS": "",
        "DATE_TIME": "2017-01-08T05:48:46.356Z",
        "S_ID": "56ad3bd7-854b-42e9-8443-46c514eb2ece",
        "C_ID": "",
        "C_NAME": "",
        "P_ID": "",
        "P_NAME": "",
        "CART_ID": "597c21a336c81d38dc77c62f",
        "O_ID": "",
        "id": "597c21be36c81d38dc77c631"
       }
    ]

var result = arr.reduce((result, date) => {
  var occurances = arr1.filter((obj) => {
    let d = new Date(obj['DATE_TIME']);
    var day = d.getDate();
    var month = d.getMonth() + 1;
    var year = d.getFullYear();

    if(day < 10){
        day='0'+day;
    } 
    if(month<10){
        month='0'+month;
    } 
    var newDate = month+'-'+day+'-'+year;
   
    return date === newDate;
  }).length;
  result[date] = occurances;
  return result;
},{});

console.log(result);

答案 3 :(得分:0)

假设日期部分具有固定数量的字符似乎是安全的。因此:

arr1.reduce((acc, item) => {
  const key = item.DATE_TIME.substr(0,10);
  if (arr.hasOwnProperty(key)) { acc[key] = (acc[key] || 0) + 1 }
  return acc;
}, {})