gpt4 book ai didi

javascript - 我如何解构一个包含其他对象的javascript对象

转载 作者:行者123 更新时间:2023-11-30 19:38:59 26 4
gpt4 key购买 nike

我有一个 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));

关于javascript - 我如何解构一个包含其他对象的javascript对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55608622/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com