javascript – 合并具有公共元素和多个数据点的数组

我试图将两个
Javascript数组合并为一个直接Javascript的数组.

我正在努力完成以下2个问题中提出的问题.但是,我的数据有几点需要合并而不是单个项目;并且数组之间的公共元素完全相同.

以下是其他问题:

> Merge two arrays matching an id
> JavaScript merging objects by id

这是我的代码(来自上面列出的第一个问题提供的最后一个答案)(但显然是错误的):

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 finalArray2 = [];
        arr2.forEach(member => {
            finalArray2.push(Object.assign({}, member,
                { route: arr1.find(m => m.route === member.route).route }
            ))
        });

        console.log(finalArray2);

我真的需要结果如下所示:

let arr3 = [
    { route: 'x1', pattern: ['y1','y2'] },
    { route: 'x2', pattern: ['y3','y4'] },
    { route: 'x3', pattern: ['y5','y6'] },
    { route: 'x4', pattern: ['y7','y8'] },
    { route: 'x5', pattern: ['y9','y10'] }
]

因此它可以在如下表格中呈现:

<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>

最佳答案 您可以使用
Map来保持对目标对象的引用.

var array1 = [{ route: 'x1' }, { route: 'x2' }, { route: 'x3' }, { route: 'x4' }, { route: 'x5' }],
    array2 = [{ 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 = array1.map(o => (routes.set(o.route, {}), Object.assign(routes.get(o.route), o, { pattern: [] })));

array2.forEach(o => routes.get(o.route).pattern.push(o.pattern));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
点赞