从 JSON 格式填充 JavaScript 数组
Populate JavaScript Array from JSON Format
您好,我有 JSON 如下所示的数据:
[
{"Mean of Arrivals for each hour":
{"03-04":-35.62,"04-05":-3.62,"05-06":-3.7}
}
]
我想将它推送到一个数组,并使该数组看起来像这样:
[
{x: 03-04 y: -35.62},
{x: 04-05, y: -3.62},
{x: 05-06, y: -3.7}
]
现在,它看起来像下面的数组,我不知道如何将它格式化为上面的样子。下面也是我用来填充数组的代码。希望获得有关如何格式化数组的帮助。
[
x: {03-04: -35.62, 04-05: -3.62, 05-06: -3.7},
y: {03-04: -35.62, 04-05: -3.62, 05-06: -3.7}
]
var dataPoints = []; // final array with data
var chartData = new google.visualization.DataTable();
chartData.addColumn('string', 'Stunde');
chartData.addColumn('number', 'Ankunft');
function addData(data) {
for (var i = 0; i < data.length - 1; i++) {
dataPoints.push({
x: data[i]["Mean of Arrivals for each hour"],
y: data[i]["Mean of Arrivals for each hour"]
});
}
dataPoints.forEach(function (row) {
chartData.addRow([row.x, row.y]);
});
console.log(dataPoints);
var chart = new google.charts.Bar(document.getElementById('plot3'));
chart.draw(chartData, google.charts.Bar.convertOptions(options));
};
$.getJSON(url, addData);
这会将所有内容展平为单个数据数组,然后通过 map() 处理对象。它将在对象内容纳任意数量的数组,以及这些数组内的任意数量的坐标集。
let obj = [
{"Mean of Arrivals for each hour":
{"03-04":-35.62,"04-05":-3.62,"05-06":-3.7}
}
]
let result = obj.map(Object.values).flat().map(Object.entries).flat().map(e => ({x:e[0], y:e[1]}))
console.log(result)
使用 for...in
循环遍历属性,构造 JSON 并推送到数组:
const data = [
{"Mean of Arrivals for each hour":
{"03-04":-35.62,"04-05":-3.62,"05-06":-3.7}
}
]
const result = [], r = data[0]["Mean of Arrivals for each hour"];
for(const key in r) result.push({x: key, y: r[key]})
console.log(result);
迭代数组中子对象的 Object.entries() 并为每个条目创建一个新对象
const data = [
{"Mean of Arrivals for each hour":
{"03-04":-35.62,"04-05":-3.62,"05-06":-3.7}
}
]
const key = "Mean of Arrivals for each hour",
res = [];
data.forEach(o => Object.entries(o[key]).forEach(([x, y]) => res.push({x, y})))
console.log(res)
您好,我有 JSON 如下所示的数据:
[
{"Mean of Arrivals for each hour":
{"03-04":-35.62,"04-05":-3.62,"05-06":-3.7}
}
]
我想将它推送到一个数组,并使该数组看起来像这样:
[
{x: 03-04 y: -35.62},
{x: 04-05, y: -3.62},
{x: 05-06, y: -3.7}
]
现在,它看起来像下面的数组,我不知道如何将它格式化为上面的样子。下面也是我用来填充数组的代码。希望获得有关如何格式化数组的帮助。
[
x: {03-04: -35.62, 04-05: -3.62, 05-06: -3.7},
y: {03-04: -35.62, 04-05: -3.62, 05-06: -3.7}
]
var dataPoints = []; // final array with data
var chartData = new google.visualization.DataTable();
chartData.addColumn('string', 'Stunde');
chartData.addColumn('number', 'Ankunft');
function addData(data) {
for (var i = 0; i < data.length - 1; i++) {
dataPoints.push({
x: data[i]["Mean of Arrivals for each hour"],
y: data[i]["Mean of Arrivals for each hour"]
});
}
dataPoints.forEach(function (row) {
chartData.addRow([row.x, row.y]);
});
console.log(dataPoints);
var chart = new google.charts.Bar(document.getElementById('plot3'));
chart.draw(chartData, google.charts.Bar.convertOptions(options));
};
$.getJSON(url, addData);
这会将所有内容展平为单个数据数组,然后通过 map() 处理对象。它将在对象内容纳任意数量的数组,以及这些数组内的任意数量的坐标集。
let obj = [
{"Mean of Arrivals for each hour":
{"03-04":-35.62,"04-05":-3.62,"05-06":-3.7}
}
]
let result = obj.map(Object.values).flat().map(Object.entries).flat().map(e => ({x:e[0], y:e[1]}))
console.log(result)
使用 for...in
循环遍历属性,构造 JSON 并推送到数组:
const data = [
{"Mean of Arrivals for each hour":
{"03-04":-35.62,"04-05":-3.62,"05-06":-3.7}
}
]
const result = [], r = data[0]["Mean of Arrivals for each hour"];
for(const key in r) result.push({x: key, y: r[key]})
console.log(result);
迭代数组中子对象的 Object.entries() 并为每个条目创建一个新对象
const data = [
{"Mean of Arrivals for each hour":
{"03-04":-35.62,"04-05":-3.62,"05-06":-3.7}
}
]
const key = "Mean of Arrivals for each hour",
res = [];
data.forEach(o => Object.entries(o[key]).forEach(([x, y]) => res.push({x, y})))
console.log(res)