首页 > 解决方案 > 如何在javascript中解决这个问题?

问题描述

> Locate the `displayBirthdate` function you initially defined, which took no parameter. Modify it to use object de-structuring to get just the 'dob' property of the parameter object it will receive

here's the code 
```javascript
const displayBirthdate = () => {};   
const displayAddress = () => {};  
const displayExtraUserInfo = (extra) => {  
    document.getElementById("btn-birthdate").addEventListener("click", ()=>   
        { displayBirthdate(extra) })
    document.getElementById("btn-phone").addEventListener("click", () => 
        { displayPhone(extra) }) 
    document.getElementById("btn-address").addEventListener("click", () => 
        { displayAddress(extra) })
```

添加到上述问题,这是作为额外参数传递的预期响应

{
  "results": [
    {
      "gender": "female",
      "name": {
        "title": "ms",
        "first": "ceyhan",
        "last": "dizdar"
      },
      "location": {
        "street": "3826 şehitler cd",
        "city": "tekirdağ",
        "state": "bitlis",
        "postcode": 11689,
        "coordinates": {
          "latitude": "79.1017",
          "longitude": "27.1350"
        },
        "timezone": {
          "offset": "+5:45",
          "description": "Kathmandu"
        }
      },
      "email": "ceyhan.dizdar@example.com",
      "login": {
        "uuid": "34eb65b2-0535-4656-bd68-4da69dc6d016",
        "username": "orangefish864",
        "password": "grandpa",
        "salt": "vowzvAS2",
        "md5": "cf4a7f3210ef97e8e72defafd80b94c8",
        "sha1": "4f2af3439862b9bf25757ee73df8cd410ce201a2",
        "sha256": 
        "1497acbca446b5fa47d4bc5ffe4e82c17818176596b66d94f213f091c8ed8077"
      },
      "dob": {
        "date": "1979-08-10T22:03:55Z",
        "age": 39
      },
      "registered": {
        "date": "2008-05-24T13:30:20Z",
        "age": 10
      },
      "phone": "(873)-801-4132",
      "cell": "(751)-606-5317",
      "id": {
        "name": "",
        "value": null
      },
      "picture": {
        "large": "https://randomuser.me/api/portraits/women/59.jpg",
        "medium": "https://randomuser.me/api/portraits/med/women/59.jpg",
        "thumbnail": 
        "https://randomuser.me/api/portraits/thumb/women/59.jpg"
      },
      "nat": "TR"
    }
  ],
  "info": {
    "seed": "008a9fe3a638239b",
    "results": 1,
    "page": 1,
    "version": "1.2"
  }
}

现在的问题是:编写一个箭头函数示例 displayBirthdate(),传入这个对象(额外)作为参数。使用解构方法,获取对象中的“dob”属性(额外)。以下是我试图解决这个问题的方法:

const displayBirthdate = (obj) =>{
        const{results} = obj;
        const[{dob}] = results;
      }

但这似乎是不正确的。请任何帮助将不胜感激。谢谢你

标签: destructuring

解决方案


const displayBirthdate = ({dob}) => {};
const displayAddress ({location}) =>{};

推荐阅读