0

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>
4
  • 1
    before merge to arrays in javascript, becose n-repeat only need 1 array, and in your need it cant done like you want. Commented Feb 14, 2017 at 10:23
  • 1
    You can nest ng-repeat's. You can use ng-if while parsing arrayB if you need to display arrayB values depending on what value a.id has. Also, make sure you use the "track by" option for your ng-repeats. Commented Feb 14, 2017 at 10:28
  • Can you provide code sample for this? Commented Feb 14, 2017 at 10:28
  • @JavaUser — can you please provide the structure of your arrays (your controller's code)? Commented Feb 14, 2017 at 10:29

1 Answer 1

2

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.