我正在使用 d3.js 图表库,并且我正在使用雷达图 extension它似乎只以一种方式接受数据对象。我没有尝试更改扩展的代码,而是认为将我的数据处理成其编码可接受的类型会更容易。如果有道理的话?
无论如何,这是我的代码;
我的 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 中,但没有返回所需的效果。
我希望这是可能的,非常感谢任何建议!
这是一个已设置好的 plnk -
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);
});
关于javascript - 如何使用 javascript 将 JSON 对象操作为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37634175/