簡體   English   中英

我如何破壞一個包含其他對象的 javascript 對象

[英]How can i destucture a javascript object containing other objects within it

我有一個 javascript 對象k ,我通過解構從中獲取了profile 新對象在其中包含其他對象(即,包括位置、登錄...並且位置也包含其他對象)。 我現在遇到的問題是打印出性別(或解構對象中的任何其他屬性),它僅在我省略用於解構配置文件數組的行中的位置時才有效。 拜托,我需要有關如何正確解決此問題的幫助。

let k = {
"results": [
{
  "gender": "male",
  "name": {
    "title": "mr",
    "first": "eliott",
    "last": "roussel"
  },
  "location": {
    "street": "9072 rue de l'abbé-migne",
    "city": "versailles",
    "state": "indre",
    "postcode": 83762,
    "coordinates": {
      "latitude": "-4.2370",
      "longitude": "-139.6080"
    },
    "timezone": {
      "offset": "-8:00",
      "description": "Pacific Time (US & Canada)"
    }
  },
  "email": "eliott.roussel@example.com",
  "login": {
    "uuid": "3e2e9f7d-ca08-4c81-93b2-f42dd0bbb421",
    "username": "saddog976",
    "password": "chippy",
    "salt": "XPbHVGge",
    "md5": "70ab9b8e14cc0be868dc53995274f5b9",
    "sha1": "4cedb04743f2529ea2a801ec539f8f02731f659d",
    "sha256": "f2c6c1bcdcd0cc4de923501cd5bf87dbd1d948c95abc9f9b08a90b78ae7f5616"
  },
  "dob": {
    "date": "1960-08-08T00:53:25Z",
    "age": 58
  },
  "registered": {
    "date": "2004-07-15T15:57:51Z",
    "age": 14
  },
  "phone": "05-07-77-21-18",
  "cell": "06-64-48-62-07",
  "id": {
    "name": "INSEE",
    "value": "1NNaN32524474 85"
  },
  "picture": {
    "large": "https://randomuser.me/api/portraits/men/81.jpg",
    "medium": "https://randomuser.me/api/portraits/med/men/81.jpg",
    "thumbnail": "https://randomuser.me/api/portraits/thumb/men/81.jpg"
  },
  "nat": "FR"
}
],
"info": {
"seed": "d8ec8b34c6f6c368",
"results": 1,
"page": 1,
"version": "1.2"
 }
}
let l = k.results;
let profile = l[0];
const{gender, name, email, location, login, dob, registered, phone, cell, id, picture, nat} = profile;

alert(gender);

您可以重命名namelocation屬性,因為它們都是內置對象的一部分。

 var k = { results: [{ gender: "male", name: { title: "mr", first: "eliott", last: "roussel" }, location: { street: "9072 rue de l'abbé-migne", city: "versailles", state: "indre", postcode: 83762, coordinates: { latitude: "-4.2370", longitude: "-139.6080" }, timezone: { offset: "-8:00", description: "Pacific Time (US & Canada)" } }, email: "eliott.roussel@example.com", login: { uuid: "3e2e9f7d-ca08-4c81-93b2-f42dd0bbb421", username: "saddog976", password: "chippy", salt: "XPbHVGge", md5: "70ab9b8e14cc0be868dc53995274f5b9", sha1: "4cedb04743f2529ea2a801ec539f8f02731f659d", sha256: "f2c6c1bcdcd0cc4de923501cd5bf87dbd1d948c95abc9f9b08a90b78ae7f5616" }, dob: { date: "1960-08-08T00:53:25Z", age: 58 }, registered: { date: "2004-07-15T15:57:51Z", age: 14 }, phone: "05-07-77-21-18", cell: "06-64-48-62-07", id: { name: "INSEE", value: "1NNaN32524474 85" }, picture: { large: "https://randomuser.me/api/portraits/men/81.jpg", medium: "https://randomuser.me/api/portraits/med/men/81.jpg", thumbnail: "https://randomuser.me/api/portraits/thumb/men/81.jpg" }, nat: "FR" }], info: { seed: "d8ec8b34c6f6c368", results: 1, page: 1, version: "1.2" } }, { gender, name: name2, email, location: loc2, login, dob, registered, phone, cell, id, picture, nat } = k.results[0]; console.log(gender);

如果你不喜歡重命名它,你可以將解構移動到一個自己的函數中,並將變量用作局部變量。

 function process({ gender, name, email, location, login, dob, registered, phone, cell, id, picture, nat }) { return gender; } var k = { results: [{ gender: "male", name: { title: "mr", first: "eliott", last: "roussel" }, location: { street: "9072 rue de l'abbé-migne", city: "versailles", state: "indre", postcode: 83762, coordinates: { latitude: "-4.2370", longitude: "-139.6080" }, timezone: { offset: "-8:00", description: "Pacific Time (US & Canada)" } }, email: "eliott.roussel@example.com", login: { uuid: "3e2e9f7d-ca08-4c81-93b2-f42dd0bbb421", username: "saddog976", password: "chippy", salt: "XPbHVGge", md5: "70ab9b8e14cc0be868dc53995274f5b9", sha1: "4cedb04743f2529ea2a801ec539f8f02731f659d", sha256: "f2c6c1bcdcd0cc4de923501cd5bf87dbd1d948c95abc9f9b08a90b78ae7f5616" }, dob: { date: "1960-08-08T00:53:25Z", age: 58 }, registered: { date: "2004-07-15T15:57:51Z", age: 14 }, phone: "05-07-77-21-18", cell: "06-64-48-62-07", id: { name: "INSEE", value: "1NNaN32524474 85" }, picture: { large: "https://randomuser.me/api/portraits/men/81.jpg", medium: "https://randomuser.me/api/portraits/med/men/81.jpg", thumbnail: "https://randomuser.me/api/portraits/thumb/men/81.jpg" }, nat: "FR" }], info: { seed: "d8ec8b34c6f6c368", results: 1, page: 1, version: "1.2" } }, { results: { 0: profile } } = k; console.log(process(profile));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM