У меня есть два таких объекта:
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}]}
(объект 2 отсутствует 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/lodash@4.17.11/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}]
}