Given a map of key value pairs that I cannot control, I am tasked with converting them into an array of objects in a specific format.
let paramArray1 = [];
let paramArray2 = [];
paramArray1.push({username:"test"})
paramArray1.push({password:"test"})
paramArray1.push({contentType:"application/json"})
let map = new Map();
map.set("username","test");
map.set("password","test");
map.set("contentType","application/json");
for (const [key,value] of map.entries()){
paramArray2.push({key,value})
}
console.log(paramArray1);
console.log(paramArray2);
Upon running the provided code, you will observe two outputs. While both arrays contain the same elements, the second array needs to match the format of the first one.
Is there a way to convert a map of key-value pairs into an array without separating them into key: [key], value: [value], and instead maintain the {key:value} format seen in the first array?
If you execute the code, you will encounter the issue. Thank you for your help!
I attempted to change the comma to a colon, but this resulted in using the word "key" instead of the actual key. The output for the modified code was: [{key:test, key:test, key:application/json}]
for (const [key,value] of map.entries()){
paramArray2.push({key:value})
}