Search code examples
javascriptarraysobjectfindresponse

Get value from object/array


I need to get the token only in a variable in JS.

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

This is my response saved in a variable but I only need to get the "String" value from the token


Solution

  • If you have stored this response in a variable E.g.:

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

    You can extract the value from the "token" property like this

    let tokenFromObject = response.token
    

    or

    let tokenFromObject = response["token"]
    

    or

    let { token } = response