0

I have JSON Data from an API which contains users at different locations. I want to map it to an HTML table that looks like the one shared below.

HTML TABLE: HTML TABLE

JSON DATA:

   {
    "data": [{

        "location": "Location 1",
        "Address": "location 1, Address",
        "users": [{
            "name": "Location 1 User",
            "state": "Active"
        }]

    }, {
        "location": "Location 2",
        "Address": "Location 2, Address",


        "users": [{
                "name": "Location 2 User",
                "state": "Active"
            },
            {
                "name": "Location 2 User",
                "state": "Inactive"
            }
        ]

    }]
}

If you notice in the third column, both users of the second location are added inside one td element and are listed in one cell. I want to transform my JSON to this sort of table with RAW Javascript.

I have tried different ways but no luck so any help on this would be appreciated!

Thank you!

4
  • 2
    What have you already tried? Commented Jul 4, 2018 at 20:47
  • 1
    Definitely get a lot more help showing what you tried that can be tweaked rather than expecting others to do all the code writing for you...which won't happen since this isn't a free code writing service or a "how to" tutorial service Commented Jul 4, 2018 at 20:51
  • please show your best existing attempt, we can't fix code we can't see. Commented Jul 4, 2018 at 20:54
  • 1
    Guys! I'm not looking for code fixes / examples to steal away. I wanted a generic / simple answer to know how it is regularly done since the API / JSON I shared is not antique. The original API I have is quite different from the example code I shared but the structure where I'm stuck is the same. Just need a direction on the approach that's more suitable for handling this. Here's What I did: Looped through the data array and filtered Location objects Tried to run another loop to filter out users object details Problem I was having was have each user on individual HTML TD tag. Commented Jul 5, 2018 at 0:53

1 Answer 1

2
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <table>
        <thead>
            <tr>
                <th colspan="2">Location Name</th>
                <th colspan="2">Address</th>
                <th colspan="2">User Working</th>
            </tr>
        </thead>

        <tbody>

        </tbody>
    </table>
</body>
</html>

<script>
    json_data = 
        {
            "data": [{

                "location": "Location 1",
                "Address": "location 1, Address",
                "users": [{
                    "name": "Location 1 User",
                    "state": "Active"
                }]

            }, {
                "location": "Location 2",
                "Address": "Location 2, Address",


                "users": [{
                        "name": "Location 2 User",
                        "state": "Active"
                    },
                    {
                        "name": "Location 2 User",
                        "state": "Inactive"
                    }
                ]

            }]
        } 
    //Accessing the JSON Document using these loops
    Object.keys(json_data['data']).forEach(key => {
        var tr = document.createElement('tr');
        tr.className = 'cell'
        document.getElementsByTagName('tbody')[0].appendChild(tr);
        Object.keys(json_data['data'][key]).forEach(key2 => {
            var td = document.createElement('td');
            td.setAttribute('colspan', '2');
            if (key2 === 'users') {
                Object.keys(json_data['data'][key][key2]).forEach(key3 => {
                    if (key3 > 0) {
                        td.innerHTML = td.innerHTML + ', ' + json_data['data'][key][key2][key3]['name'];
                    }
                    else {
                        td.innerHTML = td.innerHTML + json_data['data'][key][key2][key3]['name'];
                    }

                })
            }
            else {
                td.innerHTML = json_data['data'][key][key2];
            }
            document.getElementsByClassName('cell')[key].appendChild(td);
        })
    });


</script>

the code is pretty straightforward. The main thing to take note of is the Object.keys().forEach() loops and the createElements

There could be a much easier way to do this, but hey, it works.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Mannaroth! I appreciate your help with clearing my mind on what / how to approach to map correctly. I'm making this as correct answer because something that works, can be optimized :) Thanks again for all the efforts!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.