简体   繁体   English

如何处理此JSON数组,以删除数组的每个元素中的包装器对象? 使用JavaScript

[英]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.

相关问题 如何遍历对象数组然后删除JavaScript中的每个元素 - How to iterate over an array of object then remove each element in JavaScript 如何在javascript中使用json对象删除json对象数组? - How to remove json array of object using json object in javascript? Javascript - 如何从对象数组中删除覆盖/包装对象 - Javascript - How do i remove a covering/wrapper object from an array of object 如何从网页获取JSON对象数组并将每个元素添加到HTML中的行? - How can i get an array of JSON object from a webpage and add each element to the row in HTML? 如何从JavaScript中的数组元素中删除对象 - how to remove object from array element in javascript 如何使用javascript / jQuery从数组中删除元素? - How can I remove an element from an array with javascript / jQuery? Javascript 如果 object 在数组中,我如何调用 object 的元素? - Javascript how can I call an element of an object if the object is inside an array? 如何使用Javascript Fetch API将超链接添加到返回数组的每个元素? - using Javascript fetch api how can I add a hyperlink to each element o f returned array? Javascript对象数组然后删除元素 - Javascript Object to array then remove element 如果未使用javascript定义元素,则从嵌套数组中删除数组对象 - remove array object from nested array if element is undefined using javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM