javascript 在javascript中映射对象的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45957670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 06:20:31  来源:igfitidea点击:

string to map object in javascript

javascript

提问by VAMSEE MOHAN KRISHNA

var map = new Map();
map.set('key1','value1');
map.set('key2','value2');

console.log(map);
console.log(map.toString());
console.log(JSON.parse(map.toString()))
//Uncaught SyntaxError: Unexpected token o in JSON at position 1

Converted map object to string using toString()and now I am unable to convert to map object from string.

使用toString()将地图对象转换为字符串,现在我无法从字符串转换为地图对象。

回答by Koushik Chatterjee

To store a stringified result, better to use plain JSON object, however using Mapyou can create a array of entries and stringify that

要存储字符串化的结果,最好使用纯 JSON 对象,但是使用Map您可以创建一个条目数组并将其字符串化

var str = JSON.stringify(Array.from( map.entries()));

and then again you can parse the JSON string to array and construct a new Map

然后你可以再次将 JSON 字符串解析为数组并构造一个新的 Map

var map = new Map(JSON.parse(str))

var map1 = new Map();
map1.set('key1','value1');
map1.set('key2','value2');

var str = JSON.stringify(Array.from( map1.entries()));

//store the string somewhere or pass it to someone
//or however you want to carry it

//re construct the map again
var map2 = new Map(JSON.parse(str))

console.log('key1', map2.get('key1'));
console.log('key2', map2.get('key2'));

However, Array.from(map), or using will also return the same thing and can be used here, but someone cannot grantee what it's actually returns until execute it, on the other hand, getting an Iterator and then forming an array is more conventional and readable, however Array.from(map) might be a better solution. Also spread operatorcan be used over map[...map]or map.entries()[...map.entries()]to form the same array of entries.

但是,Array.from(map), or using 也将返回相同的东西并且可以在这里使用,但是在执行它之前,有人无法授予它实际返回的内容,另一方面,获取 Iterator 然后形成数组更常规和可读,但是 Array .from(map) 可能是更好的解决方案。也spread operator可以用于map[...map]map.entries()[...map.entries()]以形成相同的条目数组。

回答by saniales

That's because the map.toString() prints [object Map]which is not parsable by JSON.parse

那是因为 map.toString() 打印[object Map]出 JSON.parse 无法解析的内容

You should use JSON.stringifyto achieve your objective

你应该JSON.stringify用来实现你的目标

Corrected script

更正的脚本

var map = new Map().set('key1','value1')
.set('key2','value2');

console.log(map);
//since the underlying type is array requires to be expanded [...map]
console.log(JSON.stringify([...map])); 

map2 = new Map(JSON.parse(JSON.stringify([...map])));

Remember: Maps are immutable objects, and the underlying type is an array, you have to reassign the map variable or chain the sets functions.

记住:Maps 是不可变对象,底层类型是一个数组,你必须重新分配 map 变量或链接集合函数。

Full explanation can be found here: http://2ality.com/2015/08/es6-map-json.html

完整的解释可以在这里找到:http: //2ality.com/2015/08/es6-map-json.html

回答by Chandrakant Thakkar

var map = new Map();
map.set('key1','value1');
map.set('key2','value2');

console.log(map);
//Covert your map object into JSON object
var jsonObj=[];

map.forEach(function(val,key){
var tempObj={};
tempObj[key]=val;
jsonObj.push(tempObj)
})

console.log(JSON.stringify(jsonObj));
console.log(JSON.parse(JSON.stringify(jsonObj)))
//Uncaught SyntaxError: Unexpected token o in JSON at position 1