【问题标题】:NodeJS combining two data with nested keysNodeJS将两个数据与嵌套键组合
【发布时间】:2021-10-06 15:57:19
【问题描述】:

我正在尝试将我的数据与键组合,但情况很奇怪。

数据: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);
});

【问题讨论】:

    标签: arrays array-merge


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2012-09-20
      • 2021-04-03
      • 1970-01-01
      • 2023-01-25
      • 2019-06-06
      • 1970-01-01
      • 2019-05-02
      • 2020-03-15
      • 2020-08-10
      相关资源
      最近更新 更多