使用JSON从AJAX调用转换D3数组数据

时间:2017-03-27 22:40:34

标签: javascript json d3.js

我有一个d3plus函数,它以这种格式获取数据,我想用一个带来JSON的AJAX调用替换它。我不确定如何将返回的JSON“转换”为等效的JavaScript数组以符合D3 / D3plus:

var data = [
{"course": "C1301_088", "studentid":"20000", "grade":82},
{"course": "C1301_088", "studentid":"20001", "grade":42},
{"course": "C1301_088", "studentid":"20002", "grade":55},
{"course": "C1301_088", "studentid":"20003", "grade":62}
]

1 个答案:

答案 0 :(得分:0)

我想了解你想要什么,但我知道你需要将json转换为数组javascript。

然后,您可以使用ECMAScript6的“for ... of”

var data = [
{"course": "C1301_088", "studentid":"20000", "grade":82},
{"course": "C1301_088", "studentid":"20001", "grade":42},
{"course": "C1301_088", "studentid":"20002", "grade":55},
{"course": "C1301_088", "studentid":"20003", "grade":62}
]
var course = [], studentid = [], grade = [];

for (let key of data){
    course.push(key.course);
    studentid.push(key.studentid);
    grade.push(key.grade);
}

console.log(course);
console.log(studentid);
console.log(grade)

JSFiddle