提问者:小点点

如何处理这个JSON数组以删除数组中每个元素中的包装器对象?使用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”:{…}包含一些其他字段的objet。

好的,我的问题是:我必须删除这些day-X对象,并将内容直接放在主{}对象中。

因此,从前面的数组开始,我必须获得如下内容:

{
    "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"
                }
            }
        }
    ]
}

什么是明智的做法?从最初的forecast数组开始,如何删除day-x包装对象并将其内容保存到{…}这个数组的对象元素?我必须在纯JavaScript中完成,不能使用第三方库或框架


共2个答案

匿名用户

使用map迭代并使用对象返回第一个值。数组中每个项目的值。

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.
}

对象。values()answer其他人提供的值也应该有效,而不是我的对象。keys()在此处进行简化,但请注意对象。values()支持较少,因为它是一种更新得多的方法。