0

I have an object that looks something like this:

{
  item1: [element1, element2],
  item2: [element1, element2],
  item3: [element1, element2],
  item4: [element1, element2]
}

What I'm trying to do is to print a table with each element of each array in a new row. This issue I'm having is how to get the contents without repeating an entire div or table with ng-repeat.

Example:

<table ng-repeat="(key, value) in accounts">
  <tr ng-repeat="element in value">
    <td>{{element}}</td>
  </tr>
</table>

Which is giving me a new table for each key. I just one table with a row for each array element. Any way to do this with angular or do I need to use javascript?

1 Answer 1

1

It'd probably be easier to mash your elements into one array, then repeat over that array:

$scope.elements = Object.keys($scope.accounts).reduce(function(arr, key) {
    return arr.concat($scope.accounts[key])
}, []);

And then the HTML:

<table>
    <tr ng-repeat="element in elements">
        <td>{{element}}</td>
    </tr>

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.