javascript - Javascript 从原始 JSON 创建自定义格式的 JSON
问题描述
我有一个对象数组(data
),例如:
data = [
{
"stid": "ABC1",
"name": "Some Name 1",
"station_reports_percentage": 98,
"station_reports_received": 200,
"station_reports_possible": 210,
"expected_freq": 15,
"sensors": 6,
"average_latency": 1.2,
"last_report_time": 201907310000
},
{
"stid": "ABC2",
"name": "Some Name 2",
"station_reports_percentage": 99,
"station_reports_received": 205,
"station_reports_possible": 210,
"expected_freq": 15,
"sensors": 5,
"average_latency": 1.5,
"last_report_time": 201907310000
},
{
"stid": "ABC3",
"name": "Some Name 3",
"station_reports_percentage": 100,
"station_reports_received": 210,
"station_reports_possible": 210,
"expected_freq": 15,
"sensors": 6,
"average_latency": 1.7,
"last_report_time": 201907310000
}]
我想data_2
使用来自 的值创建一个新数组 ( ) data
,该数组已格式化为输入表格显示。键字符串被重命名,station_reports_
变量的值被组合成一个字符串。如:
data_2 = [
{
"Station ID": "ABC1",
"Name": "Some Name 1",
"Station Reports": "98% (200 of 210)",
"Expected Frequency": 15,
"Sensors": 6,
"Average Latency": 1.2,
"Last Report Time": 201907310000
},
{
"Station ID":"ABC2",
"Name": "Some Name 2",
"Station Reports": "99% (205 of 210)",
"Expected Frequency": 15,
"Sensors": 5,
"Average Latency": 1.5,
"Last Report Time": 201907310000
},
{
"Station ID":"ABC3",
"Name": "Some Name 3",
"Station Reports": "100% (210 of 210),
"Expected Frequency": 15,
"Sensors": 6,
"Average Latency": 1.7,
"Last Report Time": 201907310000
}]
如何data_2
使用 JavaScript 创建?一个有效的解决方案是理想的,因为这将在页面加载期间动态执行。
我能够从原始值构造新字符串,例如:
test = {"Station Reports": data[0]["station_reports_percentage"] + "% (" + data[0]["station_reports_received"] + " of " + data[0]["station_reports_possible"] + ")"}
但是,我正在努力for
使用 js 进行正确的循环语法。
解决方案
此代码将帮助您获得预期的结果。
let jsonData = [{
"stid": "ABC1",
"name": "Some Name 1",
"station_reports_percentage": 98,
"station_reports_received": 200,
"station_reports_possible": 210,
"expected_freq": 15,
"sensors": 6,
"average_latency": 1.2,
"last_report_time": 201907310000
},
{
"stid": "ABC2",
"name": "Some Name 2",
"station_reports_percentage": 99,
"station_reports_received": 205,
"station_reports_possible": 210,
"expected_freq": 15,
"sensors": 5,
"average_latency": 1.5,
"last_report_time": 201907310000
},
{
"stid": "ABC3",
"name": "Some Name 3",
"station_reports_percentage": 100,
"station_reports_received": 210,
"station_reports_possible": 210,
"expected_freq": 15,
"sensors": 6,
"average_latency": 1.7,
"last_report_time": 201907310000
}
];
let label = {
"stid": "Station ID",
"name": "Name",
"expected_freq": "Expected Frequency",
"sensors": "Sensors",
"average_latency": "Average Latency",
"last_report_time": "Last Report Time"
}
let newData = [];
for (let each of jsonData) {
let newFormat = {};
for(let name in each) {
if (label[name]) {
newFormat[label[name]] = each[name];
}
}
newFormat["Station Reports"] = `${each["station_reports_percentage"]}% (${each["station_reports_received"]} of ${each["station_reports_possible"]})}`;
newData.push(newFormat);
}
console.log(newData);
推荐阅读
- google-cloud-platform - 如何在 bigquery 中下载大结果
- javascript - 返回重复数
- python - python按字符串匹配模式对列表项进行分组
- c - For循环在不应该的时候向后打印,解决方案有什么输入吗?
- python - 如何将 JSON 数据从 postman 推送到 django 框架
- angular - 对 WordPress 网站的 Angular HTTP 请求
- ruby - RubyMine 在 Ubuntu 上找不到 Ruby SDK
- python-3.x - 在python pandas的数据库表中一一加载相同的数据文件
- python - 如何在每次调用函数时缓存函数结果并更新缓存值?
- jquery - jQuery.eCalendar() - 仅在点击日期时显示事件