I have a JSON-Array and want to loop though it. I know that there is no foreach-loop (like in PHP), but is there a possibility to access the field index of the array?
This is my json:
{
'username': 'Karl',
'email': 'xyz@abc.xx',
'user_id': 5,
...
}
I have some span elements on my page with a data-field attribute. E.g:
<span data-field="username">xyz</span>
The json-array is the return of an ajax-request. With it, I want to replace every element where the data-field=""
matches the array-index.
Right know, I do it this way for each element in the json-array:
$("[data-field='username']").text(data.username);
$("[data-field='email']").text(data.email);
But with more and more elements this becomes a little bit ugly^^
Any other possibilties? I read something about transforming the json to an object, but I have no idea how to do.