obj= {
"element-id": 0,
"measurement": [{
"from": "2018-10-17T13:40:00Z",
"to": "2018-10-17T13:45:00Z",
"value": [{
"value": 9,
"label": "fw"
}, {
"value": 7,
"label": "bw"
}]
}, {
"from": "2018-10-17T13:45:00Z",
"to": "2018-10-17T13:50:00Z",
"value": [{
"value": 6,
"label": "fw"
}, {
"value": 8,
"label": "bw"
}]
}]
};
var res = foo(obj);
-- Expected res output:
[ { address: 0,
datetime: 2018-10-17T13:45:00.000Z, <- this is a javascript Date object
day: '2018-10-17',
In: 9,
Out: 7 },
{ address: 0,
datetime: 2018-10-17T13:50:00.000Z, <- this is a javascript Date object
day: '2018-10-17',
In: 6,
Out: 8 }]
函数foo
应该将对象obj
转换为另一个对象。生成的res
对象应该是一个数组,其元素的结构均相同,并且所有元素都应包含一个Date
对象。我该如何测试?
答案 0 :(得分:0)
这是我从您的帖子和评论中了解的内容。如有任何错误,请浏览我。
obj= {
"element-id": 0,
"measurement": [{
"from": "2018-10-17T13:40:00Z",
"to": "2018-10-17T13:45:00Z",
"value": [{
"value": 9,
"label": "fw"
}, {
"value": 7,
"label": "bw"
}]
}, {
"from": "2018-10-17T13:45:00Z",
"to": "2018-10-17T13:50:00Z",
"value": [{
"value": 6,
"label": "fw"
}, {
"value": 8,
"label": "bw"
}]
}]
};
let arr = [obj]
let foo = []
arr[0].measurement.map(data => {
let d = new Date()
foo.push({
address: 0,
datetime: d.toISOString(),
day: d.toISOString().slice(0,10),
in: data.value.filter(v => v.label == "fw")[0].value,
out: data.value.filter(v => v.label == "bw")[0].value
})
})
console.log(foo)