JavaUser
JavaUser

Reputation: 26344

Angular - Iterate array inside ng-repeat and fetch values from another array

I have two array objects(array A and array B) and I am building single angular table based on this. First I need to iterate over array A and populate few columns and the other columns are based on array B. I fetch the key from array A and pass the key to fetch the value from array B.

Please let me know how to achieve this in angular?

<tbody>
<tr ng-repeat="a in arrayA">
<td> <b>{{$index+1}}</b> </td>
<td> <b>{{a.id}}</b> </td>
<td> <b>{{a.name}}</b> </td>
<td> {{a.number}} </td>
<td> This value should be from arrayB . I will pass the key a.id and here i need to iterate arrayB and get corresponding value from arrayB</td>
<td> This value should be from arrayB . I will pass the key a.id and here i need to iterate arrayB and get corresponding value from arrayB</td>
</tr>
</tbody>

Upvotes: 0

Views: 749

Answers (1)

Saurabh Agrawal
Saurabh Agrawal

Reputation: 7739

Try this

controller

$scope.getValue = function (id) {
        var returnData = '';
        angular.forEach(arrayB,function(index){
            if (index.id == id) {
                returnData = index.name;
            }

        })
        return returnData
    }

html

<tbody>
    <tr ng-repeat="a in arrayA">
        <td> <b>{{$index+1}}</b> </td>
        <td> <b>{{a.id}}</b> </td>
        <td> <b>{{a.name}}</b> </td>
        <td> {{a.number}} </td>
        <td> {{getValue(a.id)}} This value should be from arrayB . I will pass the key a.id and here i need to iterate arrayB and get corresponding value from arrayB</td>
        <td> This value should be from arrayB . I will pass the key a.id and here i need to iterate arrayB and get corresponding value from arrayB</td>
    </tr>
</tbody>

Upvotes: 2

Related Questions