首页 > 解决方案 > 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);
});

标签: arraysarray-merge

解决方案


我假设它在 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);
});



推荐阅读