首页 > 解决方案 > Angular 6 *ngFor 循环通过两个数组

问题描述

我有两个数组

 firstArray= [{id: 1, name:'firstValue1'}, {id:2, name:'firstValue2'}]

 secondArray= [{ "num": 1, "fullName": SecondValue1 , id:1}]

我想显示这样的数据

firstValue1 -------->> SecondValue1

firstValue2 -------->> 

我如何在 [(ngModel)] 或输入或选择框中填充这两个数组?

感谢您的时间和回复!

标签: arraysangulartypescript

解决方案


而不是维护secondArrayArray将其转换为HashMap

例子

firstArray = [
    { id: 1, name:'firstValue1' }, 
    { id: 2, name:'firstValue2' }
];
secondArray = { 
    '1': { "num": 1, "fullName": "SecondValue1", id: 1 },
    '2': { "num": 1, "fullName": "SecondValue2", id: 2 },
}

html

<div *ngFor="let item of firstArray">
    <p>{{item.name}} --> {{secondArray[item.id]?.fullName}}</p>
</div>

推荐阅读