Search code examples
javascriptarraysangularjsangularjs-ng-repeat

Bring together arrays for ng-repeat


i have mutiple arrays and want to display them in a table with ng-repeat. How can i merge arrays like a name=[name1,name2] und type=[type1,type2] to a data=[..] so i can ng-repeat data.name and data.type?


Solution

  • In your component, you can an object as per your requirement. For example:

    const names = ['name1', 'name2', 'name3'];
    const types = ['type1', 'type2', 'type3'];
    
    const data = [];
    
    for (let i = 0; i < names.length; i++)  {
        data.push({name: names[i], type: types[i]});
    }
    
    console.log(data); // [{name: 'name1', type: 'type1'}, ... and so on]
    

    In your view, you can iterate over data to render table, for example:

    <table>
        <tr *ngFor="let item of data">
            <td>{{ item.name }}</td>
            <td>{{ item.type }}</td>
        </tr>
    <table>