簡體   English   中英

如何處理此JSON數組,以刪除數組的每個元素中的包裝器對象? 使用JavaScript

[英]How can I process this JSON array to remove a wrapper object in each element of the array? using JavaScript

我不太喜歡JavaScript,並且遇到以下問題。

我有一個像這樣的JSON文檔:

{
  "forecast": [
    {
      "day-1": {
        "forecast_date": "2017-11-23",
        "morning": {
          "weather": {
            "meteo_forecast_id": 19,
            "meteo_forecast_date_time": "2017-11-23 06:00:00",
            "meteo_forecast_description_id": 2,
            "min_temp": 26,
            "max_temp": 31,
            "meteo_forecast_description_name": "Light Rain",
            "meteo_forecast_description": "Light Rain",
            "meteo_forecast_description_audio_link": "audio_link.html",
            "icon_link": "Light_Rain.png"
          }
        },
        "afternoon": {
          "weather": {
            "meteo_forecast_id": 20,
            "meteo_forecast_date_time": "2017-11-23 12:00:00",
            "meteo_forecast_description_id": 1,
            "min_temp": 33,
            "max_temp": 27,
            "meteo_forecast_description_name": "Mostly Cloudy",
            "meteo_forecast_description": "Mostly Cloudy",
            "meteo_forecast_description_audio_link": "audio_link.html",
            "icon_link": "Mostly_Cloudy_Icon.png"
          }
        }
      }
    },
    {
      "day-2": {
        "forecast_date": "2017-11-24",
        "morning": {
          "weather": {
            "meteo_forecast_id": 22,
            "meteo_forecast_date_time": "2017-11-24 06:00:00",
            "meteo_forecast_description_id": 2,
            "min_temp": 30,
            "max_temp": 34,
            "meteo_forecast_description_name": "Light Rain",
            "meteo_forecast_description": "Light Rain",
            "meteo_forecast_description_audio_link": "audio_link.html",
            "icon_link": "Light_Rain.png"
          }
        },
        "afternoon": {
          "weather": {
            "meteo_forecast_id": 23,
            "meteo_forecast_date_time": "2017-11-24 12:00:00",
            "meteo_forecast_description_id": 2,
            "min_temp": 34,
            "max_temp": 31,
            "meteo_forecast_description_name": "Light Rain",
            "meteo_forecast_description": "Light Rain",
            "meteo_forecast_description_audio_link": "audio_link.html",
            "icon_link": "Light_Rain.png"
          }
        }
      }
    }
}

如您所見,有一個名為Forecast的數組,其中包含一些對象{} ,而該對象又包含一個“ day-X”:{...}對象,其中包含一些其他字段。

好的,我的問題是:我必須刪除這些day-X對象,並將內容直接放在main {}對象中。

因此,從上一個數組開始,我必須獲得如下內容:

{
    "forecast": [
        {
            "forecast_date": "2017-11-23",
            "morning": {
                "weather": {
                    "meteo_forecast_id": 19,
                    "meteo_forecast_date_time": "2017-11-23 06:00:00",
                    "meteo_forecast_description_id": 2,
                    "min_temp": 26,
                    "max_temp": 31,
                    "meteo_forecast_description_name": "Light Rain",
                    "meteo_forecast_description": "Light Rain",
                    "meteo_forecast_description_audio_link": "audio_link.html",
                    "icon_link": "Light_Rain.png"
                }
            },
            "afternoon": {
                "weather": {
                    "meteo_forecast_id": 20,
                    "meteo_forecast_date_time": "2017-11-23 12:00:00",
                    "meteo_forecast_description_id": 1,
                    "min_temp": 33,
                    "max_temp": 27,
                    "meteo_forecast_description_name": "Mostly Cloudy",
                    "meteo_forecast_description": "Mostly Cloudy",
                    "meteo_forecast_description_audio_link": "audio_link.html",
                    "icon_link": "Mostly_Cloudy_Icon.png"
                }
            }
        },
        {
            "forecast_date": "2017-11-24",
            "morning": {
                "weather": {
                    "meteo_forecast_id": 22,
                    "meteo_forecast_date_time": "2017-11-24 06:00:00",
                    "meteo_forecast_description_id": 2,
                    "min_temp": 30,
                    "max_temp": 34,
                    "meteo_forecast_description_name": "Light Rain",
                    "meteo_forecast_description": "Light Rain",
                    "meteo_forecast_description_audio_link": "audio_link.html",
                    "icon_link": "Light_Rain.png"
                }
            },
            "afternoon": {
                "weather": {
                    "meteo_forecast_id": 23,
                    "meteo_forecast_date_time": "2017-11-24 12:00:00",
                    "meteo_forecast_description_id": 2,
                    "min_temp": 34,
                    "max_temp": 31,
                    "meteo_forecast_description_name": "Light Rain",
                    "meteo_forecast_description": "Light Rain",
                    "meteo_forecast_description_audio_link": "audio_link.html",
                    "icon_link": "Light_Rain.png"
                }
            }
        }
    ]
}

有什么聰明的方法嗎? 從原始預測數組開始,如何刪除day-x包裝對象,並將其內容保留到此數組的{...}對象元素中? 我必須用純JavaScript來做,而不能使用第三方庫或框架

使用map迭代並使用Object.values中每個項目的Object.values返回第一個值。

obj.forecast = obj.forecast.map( s => Object.values(s)[0] )

演示版

 var obj = { "forecast": [ { "day-1": { "forecast_date": "2017-11-23", "morning": { "weather": { "meteo_forecast_id": 19, "meteo_forecast_date_time": "2017-11-23 06:00:00", "meteo_forecast_description_id": 2, "min_temp": 26, "max_temp": 31, "meteo_forecast_description_name": "Light Rain", "meteo_forecast_description": "Light Rain", "meteo_forecast_description_audio_link": "audio_link.html", "icon_link": "Light_Rain.png" } }, "afternoon": { "weather": { "meteo_forecast_id": 20, "meteo_forecast_date_time": "2017-11-23 12:00:00", "meteo_forecast_description_id": 1, "min_temp": 33, "max_temp": 27, "meteo_forecast_description_name": "Mostly Cloudy", "meteo_forecast_description": "Mostly Cloudy", "meteo_forecast_description_audio_link": "audio_link.html", "icon_link": "Mostly_Cloudy_Icon.png" } } } }, { "day-2": { "forecast_date": "2017-11-24", "morning": { "weather": { "meteo_forecast_id": 22, "meteo_forecast_date_time": "2017-11-24 06:00:00", "meteo_forecast_description_id": 2, "min_temp": 30, "max_temp": 34, "meteo_forecast_description_name": "Light Rain", "meteo_forecast_description": "Light Rain", "meteo_forecast_description_audio_link": "audio_link.html", "icon_link": "Light_Rain.png" } }, "afternoon": { "weather": { "meteo_forecast_id": 23, "meteo_forecast_date_time": "2017-11-24 12:00:00", "meteo_forecast_description_id": 2, "min_temp": 34, "max_temp": 31, "meteo_forecast_description_name": "Light Rain", "meteo_forecast_description": "Light Rain", "meteo_forecast_description_audio_link": "audio_link.html", "icon_link": "Light_Rain.png" } } } } ] }; obj.forecast = obj.forecast.map( s => Object.values(s)[0] ); console.log( obj ); 

假設您需要先閱讀文件:

const fs = require('fs');

fs.readFile('/path/to/file.json', (err, file) => {
  const document = JSON.parse(file);
  document.forecast = document.forecast.map((days) => {
    return Object.keys(days).reduce((day, key) => days[key], {});
  });
  // console.log(document) or do what you want.
}

Object.values()回答其他人提供的方法也可以代替我在這里提出的Object.keys()簡化,但是要注意, Object.values()支持要少得多,因為它是一種更新的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM