简体   繁体   English

填充SVG元素,处理JSON中的数据

[英]Filling SVG element, working on data from JSON

Hello I have JSON file with this structure (I have 249 countries, all with iso codes) and as you can see I want to declare iso code as var iso, and visitors, as var visitors, and then if visistors have more than 1, and less than 50, fill SVG element where ID=iso with #94d31b, but it doesn't work... I don't know what to do now. 您好,我有一个具有这种结构的JSON文件(我有249个国家/地区,所有国家/地区都使用iso代码),如您所见,我想将iso代码声明为var iso,将访问者声明为var访问者,然后如果访问者的访问量超过1,并小于50,用#94d31b填充ID = iso的SVG元素,但是它不起作用...我现在不知道该怎么办。 and before I do "if" I want to fill Canada, but console says that ca does not exist, but I have this element in SVG, and when I delete this line, and put this line into if, then I have no error, so if doesn't work, because if he did, he should say that "ca" does not exist. 在执行“如果”之前,我想填充加拿大,但是控制台说ca不存在,但是我在SVG中有此元素,并且当我删除此行并将其放入if中时,则没有错误,所以如果不起作用,因为如果他这样做了,他应该说“ ca”不存在。

You're looping through your data incorrectly. 您正在错误地遍历data You actually want to loop through the data.iso_countries array. 您实际上想遍历data.iso_countries数组。 Try: 尝试:

$.getJSON("results.json", function(data) {
    data = data.iso_countries; // the array we really want
    for(var key in data) {

As for how to access individual parts of the SVG image, see this question . 至于如何访问SVG图像的各个部分,请参阅此问题

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

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