[英]How can i destucture a javascript object containing other objects within it
I have a javascript object k
from which i obtained profile
via destructuring.我有一个 javascript 对象k
,我通过解构从中获取了profile
。 The new object contains other objects within it (ie including location, login ... and location contains other objects within it as well).新对象在其中包含其他对象(即,包括位置、登录...并且位置也包含其他对象)。 The problem I am having now is printing out the gender (or any other property within the destructured object), it only works when I omit location in the line for destructuring the profile array.我现在遇到的问题是打印出性别(或解构对象中的任何其他属性),它仅在我省略用于解构配置文件数组的行中的位置时才有效。 Please, I need assistance on how to resolve this properly.拜托,我需要有关如何正确解决此问题的帮助。
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);
You could rename name
andlocation
property, because both are parts of the built in objects.您可以重命名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);
If you do not like to rename it, you could move the destructuring into an own function and use the variables as local variables.如果你不喜欢重命名它,你可以将解构移动到一个自己的函数中,并将变量用作局部变量。
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.