我有两个这样的对象:
const object1 = {first: [{timestamp: "2018-12-09T16:00:00.000", count: 3}], second: [{timestamp: "2018-12-09T17:00:00.000", count: 2}], third: [{timestamp: "2018-12-09T18:00:00.000", count: 2}]}
const object2 = {first: [{timestamp: "2018-12-09T16:00:00.000", count: 0}], second: [{timestamp: "2018-12-09T17:00:00.000", count: 0}], third: [{timestamp: "2018-12-09T18:00:00.000", count: 3}]}
(object1
并且object2
长度可以大于1的数组,例如:
const object1 = {first: [{timestamp: "2018-12-09T16:00:00.000", count: 3}, {timestamp: "2018-12-09T16:30:00.000", count: 5}], second: [{timestamp: "2018-12-09T17:00:00.000", count: 2}], third: [{timestamp: "2018-12-09T18:00:00.000", count: 2}]}
const object2 = {first: [{timestamp: "2018-12-09T16:00:00.000", count: 0}, {timestamp: "2018-12-09T16:30:00.000", count: 0}], second: [{timestamp: "2018-12-09T17:00:00.000", count: 0}], third: [{timestamp: "2018-12-09T18:00:00.000", count: 3}]}
)
我想要一个相同格式的对象,但:
timestamp
应该是实际数据而不是字符串(所以我需要这样做new Data(timestamp)
)count
应为总和因此,预期结果是:
const res = {first: [{timestamp: 2018-12-09T16:00:00.000, count: 3}], second: [{timestamp: 2018-12-09T17:00:00.000, count: 2}], third: [{timestamp: 2018-12-09T18:00:00.000, count: 5}]}
(如果object1
和的object2
数组长度大于1:
const res = {first: [{timestamp: 2018-12-09T16:00:00.000, count: 3}, {timestamp: "2018-12-09T16:30:00.000", count: 5}], second: [{timestamp: 2018-12-09T17:00:00.000, count: 2}], third: [{timestamp: 2018-12-09T18:00:00.000, count: 5}]}
)
我mergeWith
以这种方式使用Lodash:
const merged = _.mergeWith(object1, object2, (objValue, srcValue) => [
{ count: objValue[0].count + srcValue[0].count },
])
const r = Object.entries(merged).map(([key, value], i) => {
return { number: key, timestamp: value.map(convertTimestamp) }
})
console.log('r: ', r)
在哪里convertTimestamp
:
const convertTimestamp = (d) => ({
...d,
timestamp: new Date(d.timestamp),
})
结果如下:
[
{
"number": "first",
"timestamp": [
{
"count": 3,
"timestamp": null
}
]
},
{
"number": "second",
"timestamp": [
{
"count": 2,
"timestamp": null
}
]
},
{
"number": "third",
"timestamp": [
{
"count": 5,
"timestamp": null
}
]
}
]
显然,它不起作用。它有3个问题:
object1
和object2
具有相同的键,但如果是:const object1 = {first: [{timestamp: "2018-12-09T16:00:00.000", count: 3}], second: [{timestamp: "2018-12-09T17:00:00.000", count: 2}], third: [{timestamp: "2018-12-09T18:00:00.000", count: 2}]}
const object2 = {second: [{timestamp: "2018-12-09T17:00:00.000", count: 0}], third: [{timestamp: "2018-12-09T18:00:00.000", count: 3}]}
(缺少object2 first
),该过程不起作用...
我需要帮助
这里是一个可测试的代码:
function mergeData(object1, object2) {
const merged = _.mergeWith(object1, object2, (objValue, srcValue) => [
{ count: objValue[0].count + srcValue[0].count},
])
const r = Object.entries(merged).map(([key, value], i) => {
return { number: key, timestamp: value.map(convertTimestamp) }
})
return r
}
const convertTimestamp = (d) => {
return ({
...d,
timestamp: new Date(d.timestamp),
})
}
const object1 = {first: [{timestamp: "2018-12-09T16:00:00.000", count: 3}], second: [{timestamp: "2018-12-09T17:00:00.000", count: 2}], third: [{timestamp: "2018-12-09T18:00:00.000", count: 2}]}
const object2 = {first: [{timestamp: "2018-12-09T16:00:00.000", count: 0}], second: [{timestamp: "2018-12-09T17:00:00.000", count: 0}], third: [{timestamp: "2018-12-09T18:00:00.000", count: 3}]}
const result = mergeData(object1, object2)
console.log(result)
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>
一些例子:
// example 1
const object1 = {
first: [{timestamp: "2018-12-09T16:00:00.000", count: 3}, {timestamp: "2018-12-09T16:30:00.000", count: 5}],
second: [{timestamp: "2018-12-09T17:00:00.000", count: 2}],
third: [{timestamp: "2018-12-09T18:00:00.000", count: 2}]
}
const object2 = {
first: [{timestamp: "2018-12-09T16:00:00.000", count: 0}, {timestamp: "2018-12-09T16:30:00.000", count: 0}],
second: [{timestamp: "2018-12-09T17:00:00.000", count: 0}],
third: [{timestamp: "2018-12-09T18:00:00.000", count: 3}]
}
const result = {
first: [{timestamp: "2018-12-09T16:00:00.000", count: 3}, {timestamp: "2018-12-09T16:30:00.000", count: 5}],
second: [{timestamp: "2018-12-09T17:00:00.000", count: 2}],
third: [{timestamp: "2018-12-09T18:00:00.000", count: 5}]
}
// example 2
const object1 = {
first: [{timestamp: "2018-12-09T16:00:00.000", count: 3}, {timestamp: "2018-12-09T16:30:00.000", count: 5}],
second: [{timestamp: "2018-12-09T17:00:00.000", count: 2}],
third: [{timestamp: "2018-12-09T18:00:00.000", count: 2}]
}
const object2 = {
first: [{timestamp: "2018-12-09T16:00:00.000", count: 0}],
second: [{timestamp: "2018-12-09T17:00:00.000", count: 0}],
third: [{timestamp: "2018-12-09T18:00:00.000", count: 3}]
}
const result = {
first: [{timestamp: "2018-12-09T16:00:00.000", count: 3}, {timestamp: "2018-12-09T16:30:00.000", count: 5}],
second: [{timestamp: "2018-12-09T17:00:00.000", count: 2}],
third: [{timestamp: "2018-12-09T18:00:00.000", count: 5}]
}
// example 3
const object1 = {
first: [{timestamp: "2018-12-09T16:00:00.000", count: 3}, {timestamp: "2018-12-09T16:30:00.000", count: 5}],
second: [{timestamp: "2018-12-09T17:00:00.000", count: 2}],
third: [{timestamp: "2018-12-09T18:00:00.000", count: 2}]
}
const object2 = {
second: [{timestamp: "2018-12-09T17:00:00.000", count: 0}],
third: [{timestamp: "2018-12-09T18:00:00.000", count: 3}]
}
const result = {
first: [{timestamp: "2018-12-09T16:00:00.000", count: 3}, {timestamp: "2018-12-09T16:30:00.000", count: 5}],
second: [{timestamp: "2018-12-09T17:00:00.000", count: 2}],
third: [{timestamp: "2018-12-09T18:00:00.000", count: 5}]
}
// example 4
const object1 = {
first: [{timestamp: "2018-12-09T16:00:00.000", count: 3}, {timestamp: "2018-12-09T16:30:00.000", count: 5}],
second: [{timestamp: "2018-12-09T17:00:00.000", count: 2}, {timestamp: "2018-12-09T17:30:00.000", count: 20}, {timestamp: "2018-12-09T18:00:00.000", count: 10}],
third: [{timestamp: "2018-12-09T18:00:00.000", count: 2}]
}
const object2 = {
first: [{timestamp: "2018-12-09T16:00:00.000", count: 0}, {timestamp: "2018-12-09T16:30:00.000", count: 0}],
second: [{timestamp: "2018-12-09T17:00:00.000", count: 0}, {timestamp: "2018-12-09T17:30:00.000", count: 6}, {timestamp: "2018-12-09T18:00:00.000", count: 2}],
third: [{timestamp: "2018-12-09T18:00:00.000", count: 3}]
}
const result = {
first: [{timestamp: "2018-12-09T16:00:00.000", count: 3}, {timestamp: "2018-12-09T16:30:00.000", count: 5}],
second: [{timestamp: "2018-12-09T17:00:00.000", count: 2}, {timestamp: "2018-12-09T17:30:00.000", count: 26}, {timestamp: "2018-12-09T18:00:00.000", count: 12}],
third: [{timestamp: "2018-12-09T18:00:00.000", count: 5}]
}
Lodash的_.mergeWith()
回调将键作为第三个参数。您可以使用它来决定如何合并项目:
const object1 = {first: [{timestamp: "2018-12-09T16:00:00.000", count: 3}], second: [{timestamp: "2018-12-09T17:00:00.000", count: 2}], third: [{timestamp: "2018-12-09T18:00:00.000", count: 2}]}
const object2 = {first: [{timestamp: "2018-12-09T16:00:00.000", count: 0}], second: [{timestamp: "2018-12-09T17:00:00.000", count: 0}], third: [{timestamp: "2018-12-09T18:00:00.000", count: 3}]}
const mergefn = (...args) =>
_.mergeWith({}, ...args, (o, s, k) => {
if(_.eq(k, 'timestamp')) return new Date(s);
if(_.eq(k, 'count')) return (o || 0) + s;
});
const result = mergefn(object1, object2);
console.log(result);
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句