Convert Map To Json Js Map To Json

[Solved] Convert Map To Json Js Map To Json | Java - Code Explorer | yomemimo.com
Question : convert map to object/JSON javascript

Answered by : aristeidis-pilianidis

const map1 = new Map([ ['foo', 'bar'], ['baz', 42]
]);
const obj = Object.fromEntries(map1);
// { foo: 'bar', baz: 42 }
//To get back the map:
const map2 = new Map(Object.entries(obj));

Source : https://stackoverflow.com/questions/37437805/convert-map-to-json-object-in-javascript | Last Update : Mon, 10 Jan 22

Question : map to json

Answered by : jerome-scott

{"tags":[{"tag":"p","content":"map to json"},{"tag":"textarea","content":"const map = new Map();\nmap.set('name', 'John');\nmap.set('age', 30);\n\nconst json = JSON.stringify(Array.from(map.entries()));\nconsole.log(json); \/\/ \"[[\"name\",\"John\"],[\"age\",30]]\"\n","code_language":"javascript"}]}

Source : | Last Update : Mon, 16 Jan 23

Question : convert map to json js, map to json

Answered by : sanjai-kumar-jano3p9htkmm

const mapToJson = (map) => JSON.stringify(Object.fromEntries(map));
const map = new Map([ ['user1', 'John'], ['user2', 'Kate'], ['user3', 'Peter'],
]);
const json = mapToJson(map);
// {"user1":"John","user2":"Kate","user3":"Peter"}
console.log(json);

Source : | Last Update : Fri, 19 Aug 22

Question : convert json to map js, json to map

Answered by : sanjai-kumar-jano3p9htkmm

const jsonToMap = (json) => new Map(Object.entries(JSON.parse(json)));
const json = '{"user1":"John","user2":"Kate","user3":"Peter"}';
const map = jsonToMap(json);
// Kate
console.log(map.get('user2'));
// Map(3) { 'user1' => 'John', 'user2' => 'Kate', 'user3' => 'Peter' }
console.log(map);

Source : | Last Update : Fri, 19 Aug 22

Question : js map to json

Answered by : akbarali

function createPaths(aliases, propName, path) { aliases.set(propName, path);
}
var map = new Map(), object = {};
createPaths(map, 'paths.aliases.server.entry', 'src/test');
createPaths(map, 'paths.aliases.dist.entry', 'dist/test');
map.forEach((value, key) => { var keys = key.split('.'), last = keys.pop(); keys.reduce((r, a) => r[a] = r[a] || {}, object)[last] = value;
});
console.log(object); Run code snippet

Source : https://stackoverflow.com/questions/37437805/convert-map-to-json-object-in-javascript | Last Update : Tue, 16 Nov 21

Answers related to convert map to json js map to json

Code Explorer Popular Question For Java