I have a problem I can’t figure out. I have an ajax success function that loops through a multidimensional array like so:
function onSuccessClientCustomerData(data) {
// Count length of object
var count = 0;
for (i in data) {
if (data.hasOwnProperty(i)) {
count++;
}
}
// Fetch data
var arr = new Array(data.variable0, data.variable1, data.variable2, data.variable3, data.variable4);
for(var i = 0; i< count; i++) {
var variable = $.parseJSON(arr[i]);
}
}
I each arrays have three keys and I want to populate the td:eq(1) to td:eq(2) on each row:
<table id="test">
<tr>
<td><input type="text"></td>
<td class="outputName">-</td>
<td class="outputRating">-</td>
<td class="outputTurnover">-</td>
</tr>
<tr>
<td><input type="text"></td>
<td class="outputName">-</td>
<td class="outputRating">-</td>
<td class="outputTurnover">-</td>
</tr>
</table>
How do I do that. I’ve tried som each functions but couldn’t get it to work!
Would be grateful for any help or references to read!
Thanks!
EDIT / HERE IS MY SOLUTION
function onSuccessClientCustomerData(data) {
// Count length of object
var count = 0;
for (i in data) {
if (data.hasOwnProperty(i)) {
count++;
}
}
// Fetch data
var arr = new Array(data.variable0, data.variable1, data.variable2, data.variable3, data.variable4);
for (var i = 0; i < count; i++) {
var arr_j = $.parseJSON(arr[i]);
$("#row" + i + " .key0").html(arr_j.name);
$("#row" + i + " .key1").html(arr_j.rating);
$("#row" + i + " .key2").html(arr_j.percentOfTotalIncome);
}
}
Do you need help in the part where you have to insert the values? If so:
jQuery Code
HTML Code
Test Code
I tested the code with Chrome and it worked fine. Here are the steps to reproduce it:-
Open the Elements pane using F12 and do the following:
Open the Web Console using Ctrl + Shift + J and do the following:
Give
arr_i(we’ll be using that instead ofdata) a placeholder value:Run the code to set
count:Use my function to add the values (be sure to run the code on a page that uses jQuery! Or else, use this bookmark):
And voilà! This should probably fetch me about 20 to 30 votes
by todaytomorrowthis weeknext year (hopefully) …