我在highChart中创建了一个热图。我从Web套接字获得了datajson
的回复。如何从这个json制作热图?
var datajson = [
{"type":"a","symbol":"GOO","price":"385.7110"},
{"type":"a","symbol":"ORCl","price":"444.711"},
{"type":"b","symbol":"GOO","price":"555.711"}
]
我需要
var datajson = [
[0,0,385.7110],
[0,1,444.711],
[1,0,555.711]
]
答案 0 :(得分:0)
你还没有说为什么要在数组中设置1和0,所以我无法猜测。但这是一种使用Array.prototype.map
迭代数组,修改它并获得新数组的方法。
var datajson = [
{"type":"a","symbol":"GOO","price":"385.7110"},
{"type":"a","symbol":"ORCl","price":"444.711"},
{"type":"b","symbol":"GOO","price":"555.711"}
]
// map over the datajson array returning a new array
var result = datajson.map(item => [
item.type === 'a' ? 0 : 1,
item.symbol === 'GOO' ? 0 : 1,
item.price
])
console.log(result)
答案 1 :(得分:0)
var replace = function(data) {
for (var i = 0; i < data.length; i++) {
if (data[i].type === "a") {
data[i].type = "0";
} else {
data[i].type = "1";
}
if (data[i].symbol === "GOO") {
data[i].symbol = "0";
} else {
data[i].symbol = "1";
}
}
};
var datajson = [{"type":"a","symbol":"GOO","price":"385.7110"},
{"type":"a","symbol":"ORCl","price":"444.711"},
{"type":"b","symbol":"GOO","price":"555.711"}];
replace(datajson);