首页 > 解决方案 > 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 进行正确的循环语法。

标签: javascriptarrays

解决方案


此代码将帮助您获得预期的结果。

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);


推荐阅读