[英]adding objects into an existing object
I need to load an external JSON file using a d3.json function which for eg. 我需要使用d3.json函数加载外部JSON文件,例如 has this format
具有这种格式
{"pole":[{"name": "p1"}, {"name":"p2"}]}
In the main file, I would have 在主文件中,我将拥有
var MainData ={}; d3.json("url", function(graph){MainData[graph] = graph;});
gives me the external JSON object pushed inside MainData. 给了我在MainData内部推送的外部JSON对象。 But I would lie to be able to access it as MainData.pole
但是我说谎是能够以MainData.pole的身份访问它
Also I would like to load multiple JSON files and push them into the same MainData file. 另外,我想加载多个JSON文件并将其推送到同一MainData文件中。
I am new to JS and I would very much appreciate help. 我是JS新手,非常感谢您的帮助。
var MainData ={}; d3.json("url", function(graph){MainData[graph] = graph;});
gives me the external JSON object pushed inside MainData.
给了我在MainData内部推送的外部JSON对象。
Well, sort of, but it's got a really awkward property name, most likely [object Object]
, because when you use brackets notation like that, the property name within the brackets is given as a string (for now; ES6 adds Symbol
s), so any non-string you put there will be turned into a string. 好吧,但是它有一个非常尴尬的属性名,很可能是
[object Object]
,因为当您使用这样的方括号表示法时,括号内的属性名是以字符串形式给出的(现在; ES6添加Symbol
) ,因此您放置在其中的任何非字符串都将变成字符串。 The default result of calling toString
on a plain object is [object Object]
. 在普通对象上调用
toString
的默认结果是[object Object]
。
But I would lie to be able to access it as
MainData.pole
但是我说谎是能够以
MainData.pole
身份访问它
Assuming d3.json
parses it for you before giving it to you, then: 假设
d3.json
在为您解析它之前将其提供给您,然后:
var MainData ={};
d3.json("url", function(graph) {
MainData.pole = graph.pole;
});
If you're expected to parse it yourself (which seems unlikely), then: 如果希望您自己解析(似乎不太可能),则:
var MainData ={};
d3.json("url", function(graph) {
MainData.pole = JSON.parse(graph).pole;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.