分享
三行代码  ›  专栏  ›  技术社区  ›  DGB

一种更简洁的对象结构分解方法(嵌套)?

  •  0
  • DGB  · 技术社区  · 2 天前

    到目前为止,解构看起来是这样的:

      const { response = [] } = res;
      const { weather = [], main = [] } = response;
      const { humidity, temp_min, temp_max, feels_like, temp } = main;
    
    {
      "response": {
        "coord": {
          "lon": 69.42,
          "lat": 34.5
        },
        "weather": [
          {
            "id": 500,
            "main": "Rain",
            "description": "light rain",
            "icon": "10d"
          }
        ],
        "base": "stations",
        "main": {
          "temp": 12.15,
          "feels_like": 7.43,
          "temp_min": 12.15,
          "temp_max": 12.15,
          "pressure": 1017,
          "humidity": 27,
          "sea_level": 1017,
          "grnd_level": 812
        },
        "wind": {
          "speed": 2.83,
          "deg": 77
        },
        "rain": {
          "3h": 0.72
        },
        "clouds": {
          "all": 12
        },
        "dt": 1585210208,
        "sys": {
          "country": "AF",
          "sunrise": 1585185447,
          "sunset": 1585229894
        },
        "timezone": 16200,
        "id": 1138957,
        "name": "Kabul",
        "cod": 200
      },
      "error": null
    }
    

    有办法在一条甚至两条线上做到这一点吗?

    3 回复  |  直到 2 天前
        1
  •  0
  •   palaѕн    2 天前

    您可以使用:

    const {
       response: {
          weather = [],
          main: {
             humidity,
             temp_min,
             temp_max,
             feels_like,
             temp
          } = {}
       } = {}
    } = res;
    

    或者,在一行中,比如:

    const { response: { weather = [], main: { humidity, temp_min, temp_max, feels_like, temp } = {} } = {} } = res;
    

    const res = { response: { coord: { lon: 69.42, lat: 34.5 }, weather: [{ id: 500, main: "Rain", description: "light rain", icon: "10d" }], base: "stations", main: { temp: 12.15, feels_like: 7.43, temp_min: 12.15, temp_max: 12.15, pressure: 1017, humidity: 27, sea_level: 1017, grnd_level: 812 }, wind: { speed: 2.83, deg: 77 }, rain: { "3h": 0.72 }, clouds: { all: 12 }, dt: 1585210208, sys: { country: "AF", sunrise: 1585185447, sunset: 1585229894 }, timezone: 16200, id: 1138957, name: "Kabul", cod: 200 }, error: null };
    const { response: { weather = [], main: { humidity, temp_min, temp_max, feels_like, temp } = {} } = {} } = res;
    
    console.log({ humidity, temp_min, temp_max, feels_like, temp })

    请注意,在代码的第一部分:

    const { response = [] } = res;  
    

    你已经准备好了 response 默认为空数组,但是 响应 res 实际上是一个对象,因此应该将其默认为 {} main = [] 在你的密码里。

        2
  •  0
  •   Nina Scholz    2 天前

    可以使用嵌套的析构化来获取单个表达式。

    const
        res = { response: { coord: { lon: 69.42, lat: 34.5 }, weather: [{ id: 500, main: "Rain", description: "light rain", icon: "10d" }], base: "stations", main: { temp: 12.15, feels_like: 7.43, temp_min: 12.15, temp_max: 12.15, pressure: 1017, humidity: 27, sea_level: 1017, grnd_level: 812 }, wind: { speed: 2.83, deg: 77 }, rain: { "3h": 0.72 }, clouds: { all: 12 }, dt: 1585210208, sys: { country: "AF", sunrise: 1585185447, sunset: 1585229894 }, timezone: 16200, id: 1138957, name: "Kabul", cod: 200 }, error: null },
        { 
             response: { 
                 weather = [],
                 main: { humidity, temp_min, temp_max, feels_like, temp } = {}
             } = {}
        } = res;
    
    console.log(weather);
    console.log(humidity, temp_min, temp_max, feels_like, temp);
        3
  •  -1
  •   Sohan    2 天前

    嵌套解构

    let {response: {weather = [], main: {humidity = null, temp_min = null, temp_max = null, feels_like = null, temp = null}}} = res;