var y = 0;
var a = [];
var ob = {};
for (var i = 0; i < rows.length - 1; i++) {
if (new Date(rows[i].date).getTime() === new Date(dates[y]).getTime()) {
ob.sum = parseInt(rows[i].calls);
ob.date = rows[i].date;
ob.time = rows[i].time;
a[y] = ob;
}
else {
y++
}
}
&#39;行&#39;在每个日期有许多行的对象中,并且包含来自多个日期的数据。 &#39;日期&#39;是仅包含相关日期的数组。
我想要实现的目标: 我希望匹配日期,只要有匹配
a)创建一个数组/对象
b)使用与该日期相关的所有行(如您所见,电话和时间)填充它。
c)增量&#39; y&#39;并为每个匹配日期执行此操作。我认为在尝试创建对象时我做错了,但请告知。
console.log(a[0]):
{sum: 1, date: Thu Aug 31 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), time: 12}
1
:
{sum: 1, date: Thu Aug 31 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), time: 12}
2
:
{sum: 1, date: Thu Aug 31 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), time: 12}
3
:
{sum: 1, date: Thu Aug 31 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), time: 12}
4
:
{sum: 1, date: Thu Aug 31 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), time: 12}
5
:
{sum: 1, date: Thu Aug 31 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), time: 12}
6
:
{sum: 1, date: Thu Aug 31 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), time: 12}
7
:
{sum: 1, date: Thu Aug 31 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), time: 12}
8
:
{sum: 1, date: Thu Aug 31 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), time: 12}
9
:
{sum: 1, date: Thu Aug 31 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), time: 12}
10
:
{sum: 1, date: Thu Aug 31 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), time: 12}
11
:
{sum: 1, date: Thu Aug 31 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), time: 12}
12
:
{sum: 1, date: Thu Aug 31 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), time: 12}
13
:
{sum: 1, date: Thu Aug 31 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), time: 12}
14
:
{sum: 1, date: Thu Aug 31 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), time: 12}
15
:
{sum: 1, date: Thu Aug 31 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), time: 12}
16
:
{sum: 1, date: Thu Aug 31 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), time: 12}
17
:
{sum: 1, date: Thu Aug 31 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), time: 12}
18
:
{sum: 1, date: Thu Aug 31 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), time: 12}
19
:
{sum: 1, date: Thu Aug 31 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), time: 12}
20
:
{sum: 1, date: Thu Aug 31 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), time: 12}
21
:
{sum: 1, date: Thu Aug 31 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), time: 12}
22
:
{sum: 1, date: Thu Aug 31 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), time: 12}
23
:
{sum: 1, date: Thu Aug 31 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), time: 12}
24
:
{sum: 1, date: Thu Aug 31 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), time: 12}
25
:
{sum: 1, date: Thu Aug 31 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), time: 12}
26
:
{sum: 1, date: Thu Aug 31 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), time: 12}
27
:
{sum: 1, date: Thu Aug 31 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), time: 12}
28
:
{sum: 1, date: Thu Aug 31 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), time: 12}
29
:
{sum: 1, date: Thu Aug 31 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), time: 12}
30
:
{sum: 1, date: Thu Aug 31 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), time: 12}
&#39;行&#39;是一个有数千行的对象,每天大约有一百行。唯一独特的是时间&#39;。我希望每个日期只有一个对象,
row {date: Tue Aug 01 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), calls: 1, time: 9}
1
:
row {date: Tue Aug 01 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), calls: 1, time: 9}
2
:
row {date: Tue Aug 01 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), calls: 1, time: 5}
3
:
row {date: Tue Aug 01 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), calls: 1, time: 8}
4
:
row {date: Tue Aug 01 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), calls: 1, time: 6}
5
:
row {date: Tue Aug 01 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), calls: 1, time: 3}
6
:
row {date: Tue Aug 01 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), calls: 1, time: 8}
7
:
row {date: Tue Aug 01 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), calls: 1, time: 8}
8
:
row {date: Tue Aug 01 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), calls: 1, time: 8}
9
:
row {date: Tue Aug 01 2017 02:00:00 GMT+0200 (W. Europe Daylight Time), calls: 1, time: 9}
答案 0 :(得分:0)
如果我理解正确,你想要比较两个对象阵列并将相同的对象带到另一个对象,并从这些数组中删除相同的元素,对吗?
如果是这样的话:
让我们说你的2个阵列是var arr1 = [...] ; var arr2= [...]
并假设您对所有对象都有唯一的ID
你有arr3 =[];
推送重复的对象
为了比较它们,您可以使用双for loop
之类的:
for (var i = 0; i <arr1.length ; i++){
for (var j = 0; j <arr2.length ; i++){
if( arr1[i].id === arr2[j].id) {
//Youre here if you have same object in your two arrays.
arr3.push(arr1[i]); // we are pushing the duplicated obj to arr3
arr1.splice(i,1); // removing the tobject from arr1
arr2.splice(j,1); // removing the tobject from arr2
}
}
}
希望有所帮助
答案 1 :(得分:0)
只是猜测没有提供任何错误日期,但
var ob = {};
需要在if块中,否则你只是覆盖同一个对象的属性。 - 编辑。这不是整本圣经,但它可能会有所帮助......
var y = 0;
var a = [];
var ob = {sum:parseInt(rows[0].calls), date:rows[0].date, time:rows[0].date.getTime()};
//This was just for my intellisense
//var rows = [];
//sort rows. This is very important.....
rows.sort(sortFn);
//Now that all rows are sorted, we just "seek" forward sum-ing up the call value of each row in
//ob. When we encounter two rows with different dates we can be assured there are no more rows with the previous date
for (var i = 0; i < rows.length - 1; i++) {
//var currentRow = rows[i];
//var nextRow = rows[i + 1];
for(var j = i + 1;j < rows.length;j++){
if(rows[i].date.getTime() === rows[j].date.getTime()){
ob.sum += parseInt(rows[j].calls);
}
else{
//Add and then reset our object. The below line is a cheap way to "deep copy" an object.
//It may be overkill but it'll keep from maintaining a pointer that overrites elements of "a";
a.push(JSON.parse(JSON.stringify(ob)));
ob = {sum:parseInt(rows[j].calls), date:rows[j].date, time:rows[j].getTime()};
break;
}
}
}
//Custom sort function.
function sortFn(a,b){
//You may have to fiddle with this. If the dates resolve to date objects then
//get rid of the new Date calls and just call getTime on them.
var timeA = new Date(a).getTime(), timeB = new Date(b).getTime();
if(timeA > timeB) return 1;
if(timeA < timeB) return -1;
else return 0;
}
答案 2 :(得分:0)
我会使用lodash&#39; _.intersectionBy
方法,如:
_.intersectionBy(
[{sum: 1, date: "today", time: 12}, {sum: 1, date: "tomorrow", time: 12}],
[{sum: 1, date: "today", time: 12}],
'date');
// => [{sum: 1, date: "today", time: 12}]
答案 3 :(得分:0)
更新:这给了我想要的结果:
var y = 0;
var x = 0;
var arr = [];
var ob = {};
for (var i = 0; i < rows.length - 1; i++) {
if (new Date(rows[i].date).getTime() === new Date(dates[y]).getTime() ) {
var ob = {};
for (var x; x < rows.length; x++) {
if(new Date(rows[x].date).getTime() === new Date(dates[y]).getTime() ) {
ob[x] =
{
'sum': parseInt(rows[x].calls),
'date': rows[x].date,
'time': rows[x].time
};
arr[y] = ob;
}
}
}
else {
x = i;
y++;
}
}
我每天都会得到一个包含一个对象的数组,每行包含一个对象。 我的下一个挑战是分别按时间排序每一天。 我之前已对数组进行了排序,但无法弄清楚如何使用此结构。
[
{
"0": {
"sum": 1,
"date": "2017-09-01T00:00:00.000Z",
"time": 7
},
"1": {
"sum": 1,
"date": "2017-09-01T00:00:00.000Z",
"time": 8
},
"2": {
"sum": 1,
"date": "2017-09-01T00:00:00.000Z",
"time": 9
},
"3": {
"sum": 1,
"date": "2017-09-01T00:00:00.000Z",
"time": 5
},
"4": {
"sum": 1,
"date": "2017-09-01T00:00:00.000Z",
"time": 13
},
"5": {
"sum": 1,
"date": "2017-09-01T00:00:00.000Z",
"time": 10
},
"6": {
"sum": 1,
"date": "2017-09-01T00:00:00.000Z",
"time": 6
},
"7": {
"sum": 1,
"date": "2017-09-01T00:00:00.000Z",
"time": 8
},
"8": {
"sum": 1,
"date": "2017-09-01T00:00:00.000Z",
"time": 13
},
"9": {
"sum": 1,
"date": "2017-09-01T00:00:00.000Z",
"time": 8
},
"10": {
"sum": 1,
"date": "2017-09-01T00:00:00.000Z",
"time": 11
},
"11": {
"sum": 1,
"date": "2017-09-01T00:00:00.000Z",
"time": 6
},
"12": {
"sum": 1,
"date": "2017-09-01T00:00:00.000Z",
"time": 7
},
"13": {
"sum": 1,
"date": "2017-09-01T00:00:00.000Z",
"time": 6
}
}
]