How to access nested values in JSON response as an array

yer

Using const token = response.json().token; I am able to get the following JSON:

{
    "token": "*********",
    "roles": [
        {
            "id": 4,
            "name": "User",
            "pivot": {
                "user_id": 1,
                "role_id": 4
            }
        }
    ]
}

I want to be able to access the names within the roles as an array.

Ketzak

If you just want to access the array of roles, assuming you're using a traditional response object, you can just access it the way other users have stated:

var roles = response.json().roles

As I reread the question and comments, I get the idea that the user wants to access the names within the roles as as list. Assuming so, the map function will do this nicely:

// Assuming we store response in "data"
var data = response.json();
var names = data.roles.map(function(role){return role.name});
console.log(names);
// Then "names" will look like ["User",...]

In a nutshell, map will walk the array it's called against and run the provided function against it, with the first argument being the current value it sees in the array. This function can be defined on the fly as above, or predefined and passed if the logic is complex or reused.

This is a very common use for Map, and its sibling, Reduce. Both are often used for distilling complex, variable-length data down to a simpler form.

Map documentiation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

Update: It would appear the original question was asked regarding ES6, so here's the "proper" ES6 version with proper declarations and arrow funcs:

// Assuming we store response in "data"
const data = response.json();
const names = data.roles.map(role => role.name);
console.log(names);
// Then "names" will look like ["User",...]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Access object values in array nested in JSON object

From Dev

How to parse a multiple nested JSON response into array?

From Dev

How to access a JSON Array response using JsonPath?

From Dev

How to access the value of a json array in AJAX response

From Dev

How to turn entire JSON response into array of values

From Dev

How to access a particular set of keys in array of Json values which i get as response?

From Dev

How do I access values in a d3 json file with nested array objects

From Dev

How to assign values to Nested Json array

From Dev

How to get particular values from nested JSON response?

From Dev

How to access values from json array

From Dev

How to access nested JSON array data with angular, filtered by array parameter?

From Dev

How to access nested JSON array data with angular, filtered by array parameter?

From Dev

How to access doc values of a nested array in Elasticsearch script?

From Dev

How to access and de-duplicated nested Values in an array in Node

From Dev

How to get JSON response array all index values?

From Dev

How to get array object values from JSON response?

From Dev

PHP: How to use array_filter() to filter JSON response values?

From Dev

how can I access a nested array within a json data return?

From Dev

How to access values of nested arrays?

From Dev

How to access values of nested arrays?

From Dev

how to assign values to nested object through json object array

From Dev

How to extract array values within nested JSON data

From Dev

How to access Nested array in php

From Dev

How to access values inside a multiple array JSON object?

From Dev

How to access all array values from JSON in angular

From Dev

How to access nested JSON with ReactJS?

From Dev

How can I pull an IPv4 address from a JSON response by seacrhing nested values?

From Dev

access value in array from Json response typesciprt

From Java

Access nested object JSON array with Javascript

Related Related

  1. 1

    Access object values in array nested in JSON object

  2. 2

    How to parse a multiple nested JSON response into array?

  3. 3

    How to access a JSON Array response using JsonPath?

  4. 4

    How to access the value of a json array in AJAX response

  5. 5

    How to turn entire JSON response into array of values

  6. 6

    How to access a particular set of keys in array of Json values which i get as response?

  7. 7

    How do I access values in a d3 json file with nested array objects

  8. 8

    How to assign values to Nested Json array

  9. 9

    How to get particular values from nested JSON response?

  10. 10

    How to access values from json array

  11. 11

    How to access nested JSON array data with angular, filtered by array parameter?

  12. 12

    How to access nested JSON array data with angular, filtered by array parameter?

  13. 13

    How to access doc values of a nested array in Elasticsearch script?

  14. 14

    How to access and de-duplicated nested Values in an array in Node

  15. 15

    How to get JSON response array all index values?

  16. 16

    How to get array object values from JSON response?

  17. 17

    PHP: How to use array_filter() to filter JSON response values?

  18. 18

    how can I access a nested array within a json data return?

  19. 19

    How to access values of nested arrays?

  20. 20

    How to access values of nested arrays?

  21. 21

    how to assign values to nested object through json object array

  22. 22

    How to extract array values within nested JSON data

  23. 23

    How to access Nested array in php

  24. 24

    How to access values inside a multiple array JSON object?

  25. 25

    How to access all array values from JSON in angular

  26. 26

    How to access nested JSON with ReactJS?

  27. 27

    How can I pull an IPv4 address from a JSON response by seacrhing nested values?

  28. 28

    access value in array from Json response typesciprt

  29. 29

    Access nested object JSON array with Javascript

HotTag

Archive