繁体   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