I have an object and array with a response from my server that I need to convert to another format in order to display the value on the website. Do I need to use object mapping or parse JSON for this? If so, can someone please help?
{"header":["SEOUL","BUSAN","NAMPODONG"],"data":[[38,"CAPITAL","M31"]]}
,
Convert the above to the following:
'{"SEOUL" : "38", "BUSAN" : "CAPITAL", "NAMPODONG" : "M31"}'
var finalObj = {};
response.header.forEach(function(item, index) {
finalObj[item] = response.data[0][index];
});
The code above works fine by creating a variable and looping through the header to get its value and print it in html. The header and data are retrieved from the server, so when I enter something like "A", it will look for the SEOUL header and print 38 in the table below.
key value : A
header : SEOUL BUSAN NAMPODONG
data : 38 CAPITAL M31
I have a lot of data in the database, the above is just an example. So let's say I enter B, which is not in the database, I want to see the value "Not found" printed in html. However, this code currently prints nothing, so I am unsure if it is being processed correctly.