首页 > 解决方案 > 从对象/数组中获取值

问题描述

我只需要在 JS 的变量中获取令牌。

    {
  "user": {
    "id": "string",
    "nickName": "string",
    "score": 0,
    "rank": 0
  },
  "token": "string"
}

这是我保存在变量中的响应,但我只需要从令牌中获取“字符串”值

标签: javascriptarraysobjectfindresponse

解决方案


如果您将此响应存储在变量中,例如:

let response = {
  "user": {
    "id": "string",
    "nickName": "string",
    "score": 0,
    "rank": 0
  },
  "token": "string"
}

您可以像这样从“令牌”属性中提取值

let tokenFromObject = response.token

或者

let tokenFromObject = response["token"]

或者

let { token } = response

推荐阅读