我有一个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);
答案 0 :(得分:1)
您可以重命名name
和location
属性,因为它们都是内置对象的一部分。
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));