gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 14:48:33 24 4
gpt4 key购买 nike

我有两个数组。

这是我的数组。

 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"
}
]

我需要比较两个数组。查找匹配值并进行计数。

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

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

我需要这个输出

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

Each(arr) 值是重复次数。

我不知道。

请给我建议,

谢谢

最佳答案

您可以将日期标准化为 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; }

关于javascript - 使用 javascript 比较两个数组并查找每个值的计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45431979/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com