I have two Javascript arrays that look like the following.
Array 1:
[
{ route: 'x1' },
{ route: 'x2' },
{ route: 'x3' },
{ route: 'x4' },
{ route: 'x5' }
]
Array 2:
[
{ pattern: 'y1', route: 'x1' },
{ pattern: 'y2', route: 'x1' },
{ pattern: 'y3', route: 'x2' },
{ pattern: 'y4', route: 'x2' },
{ pattern: 'y5', route: 'x3' },
{ pattern: 'y6', route: 'x3' },
{ pattern: 'y7', route: 'x4' },
{ pattern: 'y8', route: 'x4' },
{ pattern: 'y9', route: 'x5' },
{ pattern: 'y10', route: 'x5' }
]
I want to combine them into a table that looks like the following.
<style type="text/css">
.tg {border-collapse:collapse;border-spacing:0;}
.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tg .tg-yw4l{vertical-align:top}
</style>
<table class="tg">
<tr>
<th class="tg-yw4l">ROUTE</th>
<th class="tg-yw4l">PATTERN(s)</th>
</tr>
<tr>
<td class="tg-yw4l">x1</td>
<td class="tg-yw4l">y1, y2</td>
</tr>
<tr>
<td class="tg-yw4l">x2</td>
<td class="tg-yw4l">y3, y4</td>
</tr>
<tr>
<td class="tg-yw4l">x3</td>
<td class="tg-yw4l">y5, y6</td>
</tr>
<tr>
<td class="tg-yw4l">x4</td>
<td class="tg-yw4l">y7, y8</td>
</tr>
<tr>
<td class="tg-yw4l">x5</td>
<td class="tg-yw4l">y9, y10</td>
</tr>
</table>
I have the code working to generate both of the lists from API calls. I even have the function written to search array 2 based on a hard coded route and return the corresponding patterns.
function search(nameKey, myArray){
for (var i=0; i < myArray.length; i++) {
if (myArray[i].route === nameKey) {
return myArray[i];
}
}
}
var resultObject = search("x1", array2);
console.log(resultObject);
My HTML table looks like this for the first array.
<table class="table table-hover table-condensed">
<caption>Routes and Corresponding Parts</caption>
<thread>
<tr>
<th>ROUTE</th>
</tr>
</thread>
<tbody>
{{#each array1}}
<tr>
<td>{{this.route}}</td>
</tr>
{{/each}}
</tbody>
</table>
I am stumbling trying to get the second part working. The monkey wrench in the whole works, in my mind, is that this is all dynamic data.
Any help with creating a function that can take the first array and do a search on the second array for each object in the first array and spit out another array that I can reference with another "#each" statement would be greatly appreciated (if at all possible) would be greatly appreciated!
I'd love it if I could be simplistic on the HTML code with something like this.
<table class="table table-hover table-condensed">
<caption>Routes and Corresponding Parts</caption>
<thread>
<tr>
<th>ROUTE</th>
</tr>
</thread>
<tbody>
<tr>
{{#each array1}}
<td>{{this.route}}</td>
{{/each}}
{{#each queryarray2}}
<td>{{this.matchedpatterns}}</td>
{{/each}}
</tr>
</tbody>
</table>