我有两个Javascript数组,如下所示。
数组1:
[
{ route: 'x1' },
{ route: 'x2' },
{ route: 'x3' },
{ route: 'x4' },
{ route: 'x5' }
]
数组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' }
]
我想将它们组合成一个如下所示的表格。
<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>
&#13;
我有代码工作从API调用生成两个列表。我甚至根据硬编码路径将函数写入搜索数组2并返回相应的模式。
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);
对于第一个数组,我的HTML表格看起来像这样。
<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>
我磕磕绊绊试图让第二部分工作。在我看来,整个作品中的猴子扳手就是这都是动态数据。
创建一个可以获取第一个数组的函数并在第二个数组上搜索第一个数组中每个对象的任何帮助,并吐出另一个我可以用另一个数组引用的数组&#34;#each&#34;声明将非常感谢(如果可能的话)将非常感谢!
如果我对HTML代码过于简单化,我会喜欢它。
<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>
答案 0 :(得分:0)
你可以做到
let arr1 = [
{ route: 'x1' },
{ route: 'x2' },
{ route: 'x3' },
{ route: 'x4' },
{ route: 'x5' }
]
let arr2 = [
{ 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' }
]
let result = arr2.reduce((a, b)=> {
a[b.route] = a[b.route] || [];
a[b.route].push(b.pattern);
return a;
}, {});
console.log(result);
&#13;
现在你有一个对象是关键是路由,而值是一个数组
答案 1 :(得分:0)
我能够以我需要的方式工作,这是更平坦的组合,而不是键/阵列组合。
let arr1 = [{
route: 'x1'
},
{
route: 'x2'
},
{
route: 'x3'
},
{
route: 'x4'
},
{
route: 'x5'
}
]
let arr2 = [{
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'
}
]
routes = new Map,
result = arr1.map(o => (routes.set(o.route, {}), Object.assign(routes.get(o.route), o, { pattern: [] })));
arr2.forEach(o => routes.get(o.route).pattern.push(o.pattern));
console.log(result);