0

I am trying to create a Google chart with two stacked bars. One bar is last year's data, while the other bar is this year's data.

As the data comes from two different databases, I have two JSON objects that look as below. I'm not sure how I could combine the two upstream, as one is a MS SQL DB and the other a MySql. Combining it into one SQL does not seem obvious.

[["","Others","Restaurant","Retail"],["2022",3362,810,2110],["2023",0,0,0]]
[["","Event","Hotel","Other","Restaurant"],["2022",0,0,0,0],["2023",3802.5,2642,10955.44,7256]]

The categories are not complete, there could be some more.

I am trying to merge the two objects with $.merge(json1, json2); however that creates the following object:

[["","Restaurant","Retail","Supermarket"],["2022",8667,1067,1549.6],["2023",0,0,0],["","Event","Hotel","Restaurant"],["2022",0,0,0,"2023",34580,0,6839.42]]

However, I would like to have an object in this format:

[["","Event","Hotel","Restaurant","Retail","Supermarket"],["2022",0,0,8667,1067,1549.6],["2023",34580,0,6839.42,0,0]]

Is that something I have to solve with a custom loop? Or is there some helpful existing function for this kind of combination?

I checked a possible duplicate question, however these all work with single dimension and same length dimensions. I tried var jsongraph = json1.map((e, i) => json2[i]); yet that doesn't combine the two objects as desired.

Matth
  • 159
  • 1
  • 3
  • 10
  • 1
    Can you explain how you construct the result? It's not how the arrays are meant to be merged. – Unmitigated Mar 22 '23 at 05:58
  • You need to itterate on them, and map it one by one. to each index so it will be on same location. You can't just use $.merge() – Benyamin Limanto Mar 22 '23 at 05:59
  • 1
    "*JSON objects"* - start here: [What is JSON (SO)](https://stackoverflow.com/questions/383692/what-is-json-and-what-is-it-used-for) – freedomn-m Mar 22 '23 at 06:02
  • 1
    @BenyaminLimanto Ok, that is what I feared. So I will have to custom merge them with a loop. I will try. – Matth Mar 22 '23 at 06:35

0 Answers0