簡體   English   中英

處理嵌套數據數組

[英]Handling nested data arrays

我被困住了...我想使用嵌套數組中某個字段的數據。 我是新手,所以我缺少一些地方:-(如果在debug d[0].children[1].birthdate我會得到正確的值。當我嘗試繪制日期時,我只會從“父”數據。

   //Part of my code:

    circle.append("circle") 
    .style("fill", "purple")
    .attr("r", 5)
    //This get me each parent first child but I need all the children e.g Adam, Eve, Julia....
    .attr("cx", function(d,i,j) { return x(new Date(d.parent[j].birthdate));}) 

我的資料:

"parent":[
     "name": "Mum",
       "children": [
            {
                "name": "Adam",
                "birthdate": "2013-01-05"
            },
            {
                "name": "Eve",
                "birthdate": "2013-02-05"
            },
            {
                "name": "Julia",
                "birthdate": "2013-03-05"
            }
        ]
    },        
    {
        "name": "Dad",
        "children": [
            {
                "name": "Romeo",
                "birthdate": "2013-04-05"
            },
            {
                "name": "Maria",
                "birthdate": "2013-03-15"
            }
        ]
    },
    "name": "StepMom",
       "children": [
            {
                "name": "A",
                "birthdate": "2013-01-05"
            },
            {
                "name": "B",
                "birthdate": "2013-02-05"
            },
            {
                "name": "C",
                "birthdate": "2013-03-05"
            }
        ]
    }
    ]           

目前尚不清楚您要做什么,但是在您的代碼中您沒有.children 也就是說,代替

.attr("cx", function(d,i,j) { return x(new Date(d.parent[j].birthdate));})

你可能想要

.attr("cx", function(d,i,j) { return x(new Date(d.children[j].birthdate));})

另外,您應該正確解析日期,而不要依賴瀏覽器來解析日期:

.attr("cx", function(d,i,j) { return x(d3.format("%Y-%m-%d").parse(d.children[j].birthdate));})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM