I have a function which converts a set of Javascript objects into a map. However, I get an object which cannot be navigated for its values; Here is the sample json
{
"products":{
"lemonade":{
"product-type":"beverage",
"product-details":{
"calories":"129",
"product-categories":[
222,
444
]
}
},
"limeade":{
"product-type":"beverage",
"product-details":{
"calories":"220",
"product-categories":[
222,
444
],
"salesIndex":{
"percentage":1101,
"demographics":"region-1"
}
}
}
}
}
Here is my function to convert to a map:
function objectToMap(o) {
const m = new Map();
console.log(`Processing for fragment ${fragment}`);
for (const k of Object.keys(o)) {
if (o[k] instanceof Object) {
console.log(`Key is ${k} and object is ${JSON.stringify(o[k])}`);
m.set(k, objectToMap(o[k]));
} else {
console.log(`Not an object ::Key is ${k} and object is
${JSON.stringify(o[k])}`);
m.set(k, o[k]);
}
}
return m;
}
Here is how I try to use and print the map:
const m1 = objectToMap(obj.products);
printMap(m1);
function printMap(map) {
for (const k of map.keys()) {
console.log(`Current key is ${k} and value is
${map.get(k)}`);
if (map.get(k) instanceof Map) {
printMap(map.get(k));
} else {
console.log(`No it is not a map :: Key is ${k} and value is ${map.get(k)}`);
}
}
}
However, I get for some this key, salesIndex, [object Map], why are the key, values not printed?