0

I have a jagged data from a MongoDb collection like this:

$scope.data = [ 
    { "fieldA": "value-a",
      "fieldB": "value-b"
    }, { 
      "fieldA": "value-a", 
      "fieldB": "value-b", 
      "fieldC": "value-c"  
    }]

Using ...

    angular.forEach(data, function(item){
        $scope.colHeaders.push(item. ?????????)
    });

How can I get the field names as column headers and avoid duplication?

1 Answers1

0

You can use Object.keys (Not tested):

angular.forEach(data, function(item){
    $scope.colHeaders.push(Object.keys(item));
});
Himmet Avsar
  • 1,531
  • 16
  • 23
  • That worked, but came with duplications, any way to avoid that? (Still marked your answer as correct.) –  Jan 17 '15 at 22:22
  • Take a look at this question: http://stackoverflow.com/questions/9229645/remove-duplicates-from-javascript-array – Himmet Avsar Jan 17 '15 at 22:54