简体   繁体   English

为什么我无法在json数据上调用.get /如何将json转换为地图

[英]Why am I unable to call .get on json data / how do I convert the json to a map

I'm trying to write a test for a function which takes a map as a parameter, 我正在尝试为将地图作为参数的函数编写测试,

When the code runs normally outside of a test the parameter can take the form (when I call toString): 当代码在测试之外正常运行时,参数可以采用以下形式(当我调用toString时):

Map { "id": "jobs", "label": "Jobs", "nodeType": "ROOT", "childNodesURI": "jobs?owner=*", "childIds": List [], "isFetchingChildren": false, "isToggled": true, "jobName": "" }

var node = {
                id: "jobs", 
                label: "JES Jobs", 
                nodeType: "ROOT", 
                childNodesURI: "jobs?owner=*", 
                childIds: [],
                isFetchingChildren: false, 
                isToggled: true, 
                jobName: "" };
                console.log(node.get("id"));

When node.get("id") is called I get "TypeError: node.get is not a function" 调用node.get(“ id”)时,出现“ TypeError:node.get不是函数”

I'm assuming this is because there's no function .get() foo a JSON object but not truly sure... If this is the case, how do I convert or initialise the JSON as a map? 我假设这是因为没有功能.get()foo一个JSON对象,但不能真正确定...如果是这种情况,如何将JSON转换或初始化为地图?

What you are showing us is a javascript object. 您显示给我们的是一个javascript对象。 If you need to call a "key" in this case you should use theObject.theProperty like below 如果在这种情况下需要调用“键”,则应使用如下所示的theObject.theProperty

 var node = { id: "jobs", label: "JES Jobs", nodeType: "ROOT", childNodesURI: "jobs?owner=*", childIds: [], isFetchingChildren: false, isToggled: true, jobName: "" }; //Call the object property ID console.log(node.id); 

If you need a map you need to it this way : 如果您需要地图,则需要这种方式:

 var myMap = new Map(); // setting the values myMap.set("id", "jobs"); console.log(myMap.get("id")); 

If you want to create your map starting from your object you can do it like this 如果要从对象开始创建地图,则可以这样操作

 var node = { id: "jobs", label: "JES Jobs", nodeType: "ROOT", childNodesURI: "jobs?owner=*", childIds: [], isFetchingChildren: false, isToggled: true, jobName: "" }; function buildMap(obj) { let map = new Map(); Object.keys(obj).forEach(key => { map.set(key, obj[key]); }); return map; } const map = buildMap(node); console.log(map.get("id")); 

You can just access node.id using the code you have. 您可以使用node.id的代码访问node.id There is no get() method on an object literal unless you extend the prototype yourself. 除非您自己扩展原型,否则对象文字上没有get()方法。

Map should accept an iterable object as a parameter when you create one according to the docs. 根据文档创建一个Map应该接受一个可迭代的对象作为参数。 However in practice I find this generates an undefined is not a function error when using an object instead of an array. 但是在实践中,我发现当使用对象而不是数组时,这会生成undefined is not a function错误。 Also looking at the docs, the browser support is not complete ( https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map ). 同时查看文档,浏览器支持不完整( https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Map )。

For example (quoted from above link): 例如(从上面的链接引用):

[1] Starting with Chrome 31, the feature was available behind a preference. [1]从Chrome 31开始,该功能在首选项之后可用。 In chrome://flags, activate the entry “Enable Experimental JavaScript”. 在chrome:// flags中,激活“启用实验性JavaScript”条目。

Your options as I see them are; 我认为您的选择是; you can iterate over your JSON 您可以遍历JSON

var m = new Map();
var json = {'foo': 'bar'};

for(var i in json) {
    m.set(i, json[i]);
}
m.get('foo');

Or if you don't really need the Map methods stick with the object literal. 或者,如果您真的不需要Map方法,则坚持使用对象常量。

json.foo;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM