对象数组是动态的
活动可以分为四种类型:“空闲”,“向下”,“向上”,“正在等待”
如果活动状态为打开,则下一个活动状态为关闭,但活动相同。
如何转换为我的预期输出?
var arr = [
{ Activity: 'Idle', Activity_Status: 'Open', Activity_Date: "2018-09-02T12:30:00.000Z" },
{ Activity: 'Idle', Activity_Status: 'Close', Activity_Date: "2018-09-02T14:00:00.000Z"},
{ Activity: 'Down', Activity_Status: 'Open', Activity_Date: "2018-09-02T17:30:00.000Z" },
{ Activity: 'Down', Activity_Status: 'Close', Activity_Date: "2018-09-02T19:00:00.000Z"}
]
预期输出为:
[
{ Activity: 'Idle', From: "2018-09-02T12:30:00.000Z", To: "2018-09-02T14:30:00.000Z" },
{ Activity: 'Down', From: "2018-09-02T17:30:00.000Z", To: "2018-09-02T19:00:00.000Z" }
]
答案 0 :(得分:1)
我将创建一个键为“活动”值的对象,然后从结果对象获取值以产生所需的输出。
var arr = [{
Activity: 'Idle',
Activity_Status: 'Open',
Activity_Date: "2018-09-02T12:30:00.000Z"
}, {
Activity: 'Idle',
Activity_Status: 'Close',
Activity_Date: "2018-09-02T14:00:00.000Z"
}, {
Activity: 'Down',
Activity_Status: 'Open',
Activity_Date: "2018-09-02T17:30:00.000Z"
}, {
Activity: 'Down',
Activity_Status: 'Close',
Activity_Date: "2018-09-02T19:00:00.000Z"
}]
var result = arr.reduce((mem, cur) => {
var key = cur.Activity;
if (mem[key]) {
return cur['Activity_Status'] === 'Open' ? { ...mem,
[key]: { ...mem[key],
"From": cur['Activity_Date']
}
} :
{ ...mem,
[key]: { ...mem[key],
"To": cur['Activity_Date']
}
}
} else {
return cur['Activity_Status'] === 'Open' ? { ...mem,
[key]: { ...mem[key],
"From": cur['Activity_Date'],
Activity: key
}
} :
{ ...mem,
[key]: { ...mem[key],
"To": cur['Activity_Date'],
Activity: key
}
}
}
}, {});
console.log(Object.values(result));
答案 1 :(得分:1)
由于 open 状态紧随其后的是 close 状态,因此请使用简单的for
循环,通过执行{{1} },并针对每次迭代合并当前对象和下一个对象,如下所示:
i += 2
示例:
var result = [];
for(var i = 0; i < arr.length; i += 2) {
result.push(
{ Activity: arr[i].Activity, From: arr[i].Activity_Date, To: arr[i + 1].Activity_Date }
);
}
如果您想要一种实用的方法,那么:
var arr = [ { Activity: 'Idle', Activity_Status: 'Open', Activity_Date: "2018-09-02T12:30:00.000Z" }, { Activity: 'Idle', Activity_Status: 'Close', Activity_Date: "2018-09-02T14:00:00.000Z"}, { Activity: 'Down', Activity_Status: 'Open', Activity_Date: "2018-09-02T17:30:00.000Z" }, { Activity: 'Down', Activity_Status: 'Close', Activity_Date: "2018-09-02T19:00:00.000Z"} ];
var result = [];
for(var i = 0; i < arr.length; i += 2) {
result.push(
{ Activity: arr[i].Activity, From: arr[i].Activity_Date, To: arr[i + 1].Activity_Date }
);
}
console.log(result);
示例:
let result = Array.from({length: arr.length / 2}).map((_, i) =>
({ Activity: arr[i].Activity, From: arr[i].Activity_Date, To: arr[i + 1].Activity_Date })
);
答案 2 :(得分:0)
<script>
var arr = [{Activity: 'Idle',
Activity_Status: 'Open',
Activity_Date: "2018-09-02T12:30:00.000Z" },
{Activity: 'Idle',
Activity_Status: 'Close',
Activity_Date: "2018-09-02T14:00:00.000Z"},
{Activity: 'Down',
Activity_Status: 'Open',
Activity_Date: "2018-09-02T17:30:00.000Z" },
{Activity: 'Down',
Activity_Status: 'Close',
Activity_Date: "2018-09-02T19:00:00.000Z"}];
var output = [
{Activity: 'Idle', From: "", To: "" },
{Activity: 'Down', From: "", To: "" },
{Activity: 'Up', From: "", To: "" },
{Activity: 'Waiting', From: "", To: ""}
];
var i =0;
while(i < output.length){
var j = 0;
var activity = output[i]["Activity"];
while(j < arr.length){
if(arr[j]["Activity"] == activity){
if(arr[j]["Activity_Status"] == "Open"){
output[i]["From"] = arr[j]["Activity_Date"];
}else if(arr[j]["Activity_Status"] == "Close"){
output[i]["To"] = arr[j]["Activity_Date"];
}
}
j++;
}
i++;
}
console.log(output);
</script>