2016-11-21 149 views
-9

我有一個折線圖like this。但它與具有相同數據點集合的另一條線重疊。D3.js折線圖重疊問題

任何解決方案來處理這種情況。

+3

所以遍歷並追加到一個新的陣列 – epascarello

+0

我會推薦給熟悉的['Array'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Methods_2)和['Object'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object#Methods_of_the_Object_constructor)方法。 – Xufox

回答

1

迭代對象屬性並生成結果數組。

var obj = { 
 
    "rules": [{"interval": "Week 1","count": 0},{"interval": "Week 2","count": 0}], 
 
    "assets": [{"interval": "Week 1","count": 1},{"interval": "Week 2","count": 0}] 
 
}; 
 

 
var res = Object.keys(obj) // get all object keys 
 
.reduce(function(arr, k) { // iterate over keys array and generate result array 
 
    obj[k].forEach(function(v) { // iterate over inner property value array 
 
    arr.push(Object.assign({ // generate the object element 
 
     name: k 
 
    }, v)); 
 
    }); 
 
    return arr; // return the array reference 
 
}, []); 
 

 
console.log(res);

+0

運行腳本後得到未定義的不是函數錯誤 – Rakesh

+0

@Rakesh:在哪個瀏覽器中......在舊瀏覽器中,您需要使用polyfill作爲方法 –