Cagrison
Cagrison

Reputation: 190

NodeJS combining two data with nested keys

I'm trying to combine my datas with keys, but there is a weir situation.

Data: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.

Data: 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'
  }
]

I want to combine them with a single function but output is wrong. I'm getting every element as same on result.

},
        "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"
        },

I kind'a dont know what is wrong there. Here is my function. It keeps every key and adding continued keys. What should i do ?

data2.forEach((statistic) => {
  data1[statistic.label][statistic.date] = Number(statistic.score);
});

Upvotes: 1

Views: 38

Answers (1)

Shivam Gupta
Shivam Gupta

Reputation: 159

I am assuming its in JavaScript.

I have added brackets wherever needed


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);
});


Upvotes: 1

Related Questions