arrays - NodeJS将两个数据与嵌套键结合起来
问题描述
我正在尝试将我的数据与键结合起来,但情况很奇怪。
数据:1
"qrCode": {
"2021-10-03": 0,
"2021-10-04": 0,
"2021-10-05": 0,
"2021-10-06": "2",
"2021-10-07": 0,
"2021-10-08": "1",
"2021-10-09": "1"
},
"gallery": {
"2021-10-03": 0,
"2021-10-04": 0,
"2021-10-05": 0,
"2021-10-06": "2",
"2021-10-07": 0,
"2021-10-08": "1",
"2021-10-09": "1"
},
"Whatsapp": {
"2021-10-03": 0,
"2021-10-04": 0,
"2021-10-05": 0,
"2021-10-06": "2",
"2021-10-07": 0,
"2021-10-08": "1",
"2021-10-09": "1"
},
//continues like this.
数据:2
[
TextRow { score: '1', date: '2021-10-09', value: '', label: 'share' },
TextRow {
score: '5',
date: '2021-10-06',
value: '',
label: 'qrCode'
},
TextRow {
score: '1',
date: '2021-10-06',
value: '',
label: 'gallery'
},
TextRow {
score: '1',
date: '2021-10-06',
value: 'https://api.whatsapp.com/send?phone=+905448514495',
label: 'Whatsapp'
},
TextRow {
score: '1',
date: '2021-10-06',
value: 'https://twitter.com/90pixel',
label: 'Twitter'
},
TextRow {
score: '2',
date: '2021-10-06',
value: 'https://trendyol.com/jump/yeni-sezon-outdoor-erkek-spor-ayakkabi-25715-navy-p-57375516?boutiqueId=556044&merchantId=655',
label: 'Trendyol'
},
TextRow {
score: '1',
date: '2021-10-08',
value: 'https://goo.gl/maps/z3RRpzp5SgxToDv18',
label: 'Lokasyon'
}
]
我想将它们与单个函数结合起来,但输出是错误的。我得到每个元素的结果都是一样的。
},
"qrCode": {
"2021-10-03": 0,
"2021-10-04": 0,
"2021-10-05": 0,
"2021-10-06": "2",
"2021-10-07": 0,
"2021-10-08": "1",
"2021-10-09": "1"
},
"gallery": {
"2021-10-03": 0,
"2021-10-04": 0,
"2021-10-05": 0,
"2021-10-06": "2",
"2021-10-07": 0,
"2021-10-08": "1",
"2021-10-09": "1"
},
"Whatsapp": {
"2021-10-03": 0,
"2021-10-04": 0,
"2021-10-05": 0,
"2021-10-06": "2",
"2021-10-07": 0,
"2021-10-08": "1",
"2021-10-09": "1"
},
"Twitter": {
"2021-10-03": 0,
"2021-10-04": 0,
"2021-10-05": 0,
"2021-10-06": "2",
"2021-10-07": 0,
"2021-10-08": "1",
"2021-10-09": "1"
},
我有点不知道那里有什么问题。这是我的功能。它保留每个键并添加连续键。我应该怎么办 ?
data2.forEach((statistic) => {
data1[statistic.label][statistic.date] = Number(statistic.score);
});
解决方案
我假设它在 JavaScript 中。
我在需要的地方添加了括号
let data1 = {"qrCode": {
"2021-10-03": 0,
"2021-10-04": 0,
"2021-10-05": 0,
"2021-10-06": "2",
"2021-10-07": 0,
"2021-10-08": "1",
"2021-10-09": "1"
},
"gallery": {
"2021-10-03": 0,
"2021-10-04": 0,
"2021-10-05": 0,
"2021-10-06": "2",
"2021-10-07": 0,
"2021-10-08": "1",
"2021-10-09": "1"
},
"Whatsapp": {
"2021-10-03": 0,
"2021-10-04": 0,
"2021-10-05": 0,
"2021-10-06": "2",
"2021-10-07": 0,
"2021-10-08": "1",
"2021-10-09": "1"
}}
let data2 = [
{'TextRow': { score: '1', date: '2021-10-09', value: '', label: 'share' }},
{'TextRow': {
score: '1099',
date: '2021-10-06',
value: '',
label: 'qrCode'
}},
{'TextRow': {
score: '1',
date: '2021-10-06',
value: '',
label: 'gallery'
}},
{'TextRow': {
score: '1',
date: '2021-10-06',
value: 'https://api.whatsapp.com/send?phone=+905448514495',
label: 'Whatsapp'
}},
{'TextRow': {
score: '1',
date: '2021-10-06',
value: 'https://twitter.com/90pixel',
label: 'Twitter'
}},
{'TextRow': {
score: '2',
date: '2021-10-06',
value: 'https://trendyol.com/jump/yeni-sezon-outdoor-erkek-spor-ayakkabi-25715-navy-p-57375516?boutiqueId=556044&merchantId=655',
label: 'Trendyol'
}},
{'TextRow': {
score: '1',
date: '2021-10-08',
value: 'https://goo.gl/maps/z3RRpzp5SgxToDv18',
label: 'Lokasyon'
}}
]
data2.forEach((statistic) => {
const statiscData = statistic['TextRow'];
if(data1[statiscData.label])
data1[statiscData.label][statiscData.date] = Number(statiscData.score);
});
推荐阅读
- android - 将收据打印到文件中
- python - 解析字典中的嵌套列表
- java - 如何在复杂的继承层次结构中找到哪个类覆盖了函数?
- php - Delphi Indy Post 编码和 PHP
- java - 3DES , Java 代码结果与 3des 在线工具和 oracle 数据库脚本产生不同的结果
- bulkinsert - EFCore.BulkExtensions 我应该设置 BatchSize
- android-camerax - 如何使用camerax和TextureView平滑地处理方向变化?
- selenium - 在画布标签内选择元素
- c# - 如何将所有文本输入中的先前值设置为视图状态并从特定条件中检索它们
- python - 理解列表中变量的初始化