I have a javascript MAP object which is holding key as a string and value as a javascript array, each array is holding set of strings inside it. I want to convert the map of arrays into a json object in javascript.
Here is the code which i tried
function addRole() {
var jsonObjectOfMap={};
subMenuSelectedIdMap.forEach(function(items,key,subMenuSelectedIdMap){
jsonObjectOfMap[key]=JSON.stringify(items);
});
alert(JSON.stringify(jsonObjectOfMap));
I am getting the json object as like this {"1004":"[1005,1006,1023]","1007":"[1008,1053]"}
But is this json format object is valid and what i have to do if i want it the format as this: {"1004":["1005","1006","1023"]","1007":["1008","1053"]}
Please help me out
jsonObjectOfMap[key]=items;–JSON.stringify()doesn't need to be called on each individual object. It's usually only necessary to call it once on the root object –JSON.stringify(jsonObjectOfMap). It already behaves recursively and will stringify any (compatible) values contained within.