如何处理这个JSON数组以删除数组中每个元素中的包装器对象?使用JavaScript
我不太喜欢JavaScript,我有以下问题 我有一个类似以下内容的JSON文档:如何处理这个JSON数组以删除数组中每个元素中的包装器对象?使用JavaScript,javascript,json,javascript-objects,Javascript,Json,Javascript Objects,我不太喜欢JavaScript,我有以下问题 我有一个类似以下内容的JSON文档: { "forecast": [ { "day-1": { "forecast_date": "2017-11-23", "morning": { "weather": { "meteo_forecast_id": 19, "meteo_forecast_date_time": "2017-1
{
"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": [
{
"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”:{…}对象,其中包含一些其他字段
好的,我的问题是:我必须删除这些day-X对象,并将内容直接放在主{}对象中
因此,从前面的数组开始,我必须获得如下内容:
{
"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": [
{
"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中完成,并且我不能使用第三部分库或框架使用
映射
迭代并使用对象返回数组中每个项的第一个值
obj.forecast = obj.forecast.map( s => Object.values(s)[0] )
演示
var obj={
“预测”:[
{
“第一天”:{
“预测日期”:“2017-11-23”,
“上午”:{
“天气”:{
“气象预报id”:19,
“气象预报日期时间”:“2017-11-23 06:00:00”,
“气象预报描述id”:2,
“最低温度”:26,
“最高温度”:31,
“气象预报描述名称”:“小雨”,
“气象预报描述”:“小雨”,
“meteo_forecast_description_audio_link”:“audio_link.html”,
“图标链接”:“Light\u Rain.png”
}
},
“下午”:{
“天气”:{
“气象预报id”:20,
“气象预测日期时间”:“2017-11-23 12:00:00”,
“气象预报描述id”:1,
“最低温度”:33,
“最高温度”:27,
“气象预报描述名称”:“多云”,
“气象预报描述”:“多云”,
“meteo_forecast_description_audio_link”:“audio_link.html”,
“icon\u link”:“icon.png”
}
}
}
},
{
“第二天”:{
“预测日期”:“2017-11-24”,
“上午”:{
“天气”:{
“气象预报id”:22,
“气象预测日期时间”:“2017-11-24 06:00:00”,
“气象预报描述id”:2,
“最低温度”:30,
“最高温度”:34,
“气象预报描述名称”:“小雨”,
“气象预报描述”:“小雨”,
“meteo_forecast_description_audio_link”:“audio_link.html”,
“图标链接”:“Light\u Rain.png”
}
},
“下午”:{
“天气”:{
“气象预报id”:23,
“气象预测日期时间”:“2017-11-24 12:00:00”,
“气象预报描述id”:2,
“最低温度”:34,
“最高温度”:31,
“气象预报描述名称”:“小雨”,
“气象预报描述”:“小雨”,
“meteo_forecast_description_audio_link”:“audio_link.html”,
“图标链接”:“Light\u Rain.png”
}
}
}
}
]
};
obj.forecast=obj.forecast.map(s=>Object.values[0]);
控制台日志(obj)
使用map
迭代并使用Object.values从数组中的每个项返回第一个值
obj.forecast = obj.forecast.map( s => Object.values(s)[0] )
演示
var obj={
“预测”:[
{
“第一天”:{
“预测日期”:“2017-11-23”,
“上午”:{
“天气”:{
“气象预报id”:19,
“气象预报日期时间”:“2017-11-23 06:00:00”,
“气象预报描述id”:2,
“最低温度”:26,
“最高温度”:31,
“气象预报描述名称”:“小雨”,
“气象预报描述”:“小雨”,
“meteo_forecast_description_audio_link”:“audio_link.html”,
“图标链接”:“Light\u Rain.png”
}
},
“下午”:{
“天气”:{
“气象预报id”:20,
“气象预测日期时间”:“2017-11-23 12:00:00”,
“气象预报描述id”:1,
“最低温度”:33,
“最高温度”:27,
“气象预报描述名称”:“多云”,
“气象预报描述”:“多云”,
“meteo_forecast_description_audio_link”:“audio_link.html”,
“icon\u link”:“icon.png”
}
}
}
},
{
“第二天”:{
“预测日期”:“2017-11-24”,
“上午”:{
“天气”:{
“气象预报id”:22,
“气象预测日期时间”:“2017-11-24 06:00:00”,
“气象预报描述id”:2,
“最低温度”:30,
“最高温度”:34,
“气象预报描述名称”:“小雨”,
“气象预报描述”:“小雨”,
“meteo_forecast_description_audio_link”:“audio_link.html”,
“图标链接”:“Light\u Rain.png”
}
},
“下午”:{
“天气”:{
“气象预报id”:23,
“气象预测日期时间”:“2017-11-24 12:00:00”,
“气象预报描述id”:2,
“最低温度”:34,
“最高温度”:31,
“气象预报描述名称”:“小雨”,
“气象预报描述”:“小雨”,
“meteo_forecast_description_audio_link”:“audio_link.html”,
“图标链接”:“Light\u Rain.png”
}
}
}
}
]
};
obj.forecast=obj.forecast.map(s=>Object.values[0]);
控制台日志(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.
}
公司