[英]How to manipulate JSON objects into an array with javascript
我正在使用d3.js圖表庫,並且正在使用雷達圖擴展 ,該擴展似乎僅以一種方式接受數據對象。 我認為與其嘗試更改擴展名的代碼,不如將其數據處理為接受編碼的類型會更容易。 是否有意義?
無論如何,這是我的代碼;
我的JSON:
{
"questions": ["Staff is courteous and polite","Attentive staff","Modern brand","Innovative brand","Good employer","Company I trust","Place for kids and family","Place for young people","Affordable food"],
"organizations": ["MC", "BK", "KFC"],
"dates": ["Jan", "Feb", "Mar"],
"values": [ [
[40, 15, 13],
[25, 24, 14],
[1, 23, 20]] ... etc etc
Javascript:
d3.json("data.json", function(error, data) {
var newValue =[];
var yellow = [
[
{"label":"A","value":6},
{"label":"B","value":4},
{"label":"C","value":6},
{"label":"D","value":5.5},
{"label":"E","value":8},
{"label":"F","value":7},
{"label":"G","value":9},
{"label":"H","value":10},
{"label":"I","value":3.5}
]
];
if (error) {
console.log(error);
}
else {
data = data;
}
var newValue = [];
var orgS = "MC";
var dateS = "Jan";
for (var question = 0; question < data.questions.length; question++) {
var organization = data.organizations.indexOf(orgS);
var date = data.dates.indexOf(dateS);
newValue.push({
label: data.questions[question],
value: data.values[question][organization][date]
});
}
console.log(newValue);
console.log(yellow);
});
控制台輸出結構:
所以我的問題是,如何將數據像var“ yellow”(圖片的底部)輸出到控制台?
我嘗試將[]包裹在newValue.push周圍,但未返回所需的效果。
我希望這是可能的,任何建議都將不勝感激!
這是所有的設置-
https://plnkr.co/edit/EBcxa39sal0PAOJxSYKb?p=preview
(哦,我真的不確定該問題的標題是什么,歡迎編輯/建議一個新標題以更准確地描述問題)。
雖然我看不到重點...
d3.json("data.json", function(error, data) {
var newValue =[];
var yellow = [
[
{"label":"A","value":6},
{"label":"B","value":4},
{"label":"C","value":6},
{"label":"D","value":5.5},
{"label":"E","value":8},
{"label":"F","value":7},
{"label":"G","value":9},
{"label":"H","value":10},
{"label":"I","value":3.5}
]
];
if (error) {
console.log(error);
}
else {
data = data;
}
var newValue = [];
var orgS = "MC";
var dateS = "Jan";
for (var question = 0; question < data.questions.length; question++) {
var organization = data.organizations.indexOf(orgS);
var date = data.dates.indexOf(dateS);
newValue.push({
label: data.questions[question],
value: data.values[question][organization][date]
});
}
var newArrayValue = [];
newArrayValue.push(newValue);
console.log(newValue);
console.log(newArrayValue);
console.log(yellow);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.