我在项目中使用了来自 'https://randomuser.me/api/'的 "data" 对象。
{
"results": [
{
"gender": "female",
"name": {
"title": "miss",
"first": "mia",
"last": "sutton"
}
}
]
}
我从数据对象中解构了结果如下;
const {results} = data;
我该如何解构我创建的results变量,并获取其中的第一个项?我希望这个解构出来的数组项被声明为profile,它代表了从 API 调用中获取的用户的配置文件数据,我想在我的应用程序中显示它。
const {results: [{name: {title, first, last}}]} = data;
这会完全解构您列表中的第一项。 - SeblormyArray
。因此,当我记录myArray
时,我应该得到;{ "gender": "female", "name": { "title": "miss", "first": "mia", "last": "sutton" }
- Gm Emmyconst {results: [firstPerson]} = data;
。 - SeblorfirstPerson
是否表示数组项的新变量名? - Gm EmmyfirstPerson
是一个任意的名称,它将保存列表中的第一个对象。您可以根据需要随意命名它。解构声明变量,命名由您决定。 - Seblor