[英]How can I process this JSON array to remove a wrapper object in each element of the array? using JavaScript
I am not so into JavaScript and I have the following problem. 我不太喜欢JavaScript,并且遇到以下问题。
I have a JSON document like this: 我有一个像这样的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"
}
}
}
}
}
As you can see there is an array named forecast that contains some object {} which in turn contains a "day-X":{...} objet containing some other fields. 如您所见,有一个名为Forecast的数组,其中包含一些对象{} ,而该对象又包含一个“ day-X”:{...}对象,其中包含一些其他字段。
Ok, my problem is: I have to remove these day-X object and put the content directly inside the main {} object. 好的,我的问题是:我必须删除这些day-X对象,并将内容直接放在main {}对象中。
So, starting from the previous array I have to obtain something like this: 因此,从上一个数组开始,我必须获得如下内容:
{
"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"
}
}
}
]
}
What is a smart way to do it? 有什么聪明的方法吗? Starting from the original forecast array how can I delete the day-x wrapper objets and mantain theirs content into the {...} object elements of this array?
从原始预测数组开始,如何删除day-x包装对象,并将其内容保留到此数组的{...}对象元素中? I have to do it in pure JavaScript and I can't use third parts library or framework
我必须用纯JavaScript来做,而不能使用第三方库或框架
Use map
to iterate and return the first value using Object.values
from every item in the array. 使用
map
迭代并使用Object.values
中每个项目的Object.values
返回第一个值。
obj.forecast = obj.forecast.map( s => Object.values(s)[0] )
Demo 演示版
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 );
Assuming you need to read the file first: 假设您需要先阅读文件:
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.
}
The Object.values()
answer someone else provided should also work instead of my Object.keys()
reduction here, but note that Object.values()
has much less support as it's a much newer method. Object.values()
回答其他人提供的方法也可以代替我在这里提出的Object.keys()
简化,但是要注意, Object.values()
支持要少得多,因为它是一种更新的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.