3D array
// Array
var x = {
"letter": [ "a", "b", "c", "d", "e", "f", "g", "h", "i" ],
"line": [
{ "data": [ 306, 830, 377, 651, 547, 369, 300, 148, 494 ] },
{ "data": [ 88, 339, 298, 87, 96, 108, 93, 182, 64 ] },
{ "data": [ 3157, 2943, 2724, 2116, 3700, 2980, 2449, 2494, 1057 ] },
{ "data": [ 2006, 1921, 2030, 615, 273, 415, 680, 286, 730 ] }
]
};
Some variables
// Variables
var line = x.line;
var data = [];
for (var i = 0; i < line.length; i++) {
data.push(line[i].data);
}
The challenge at hand
// Problematic code (works only for fixed number of array objects, need a solution that can handle any number)
var listData = [];
for (var i = 0; i < line.length; i++) { listData.push(''); }
for (var i = 0; i < data[0].length; i++) {
listData[0] += '<li>' + data[0][i] + '</li>';
listData[1] += '<li>' + data[1][i] + '</li>';
listData[2] += '<li>' + data[2][i] + '</li>';
listData[3] += '<li>' + data[3][i] + '</li>';
}
// Attempting another approach...
var listData = [];
for (var i = 0; i < line.length; i++) {
listData.push('');
listData[i] += '<li>' + data[i][/* ??? */] + '</li>';
}
I aim to streamline the line[data]
objects into one array, enclosing each value from the data
objects in an html <li>
tag, then merging them into a single string per object. Resultantly, listData
should be structured as follows:
Desired outcome
listData == [
"<li>306</li><li>830</li><li>377</li><li>651</li><li>547</li><li>369</li><li>300</li><li>148</li><li>494</li>",
"<li>88</li><li>339</li><li>298</li><li>87</li><li>96</li><li>108</li><li>93</li><li>182</li><li>64</li>",
"<li>3157</li><li>2943</li><li>2724</li><li>2116</li><li>3700</li><li>2980</li><li>2449</li><li>2494</li><li>1057</li>",
"<li>2006</li><li>1921</li><li>2030</li><li>615</li><li>273</li><li>415</li><li>680</li><li>286</li><li>730</li>"
]
What I seek is a method that would function not just with 4 data objects, but with any original number of objects within x
.
While I managed to achieve this with the existing 4 objects, I am struggling to implement it programmatically. Any assistance provided using my current variables would be highly appreciated! Many thanks.