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.

4
  • 1
    Can you explain how you construct the result? It's not how the arrays are meant to be merged. Commented Mar 22, 2023 at 5: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() Commented Mar 22, 2023 at 5:59
  • 1
    "JSON objects" - start here: What is JSON (SO) Commented Mar 22, 2023 at 6:02
  • 1
    @BenyaminLimanto Ok, that is what I feared. So I will have to custom merge them with a loop. I will try. Commented Mar 22, 2023 at 6:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.