0

I'm stuck with foreach loop , i don't know the column Name so i can't do value.Job , and i need to display all the colums with it specific data

angular.forEach(data, function(value, key) {
    console.log( 'the  Object N° ' + i );
    'maybe another loop for all the column
    console.log( 'the column name is + ': ' +  value ??);
});     

the result should be like :

the  Object N° 1 
the column name is  Name  :  Matt 
the column name is  Job  :  Teacher

the  Object N° 2 
the column name is  Name  :  Alice
the column name is  Job  :  Doctor

Any ideas here !

I want to see both the options for JavaScript and AngularJs forEach.

6
  • 1
    Can you share a sample of how your data is structured? Commented Sep 1, 2016 at 8:56
  • What is the i var for? Commented Sep 1, 2016 at 8:57
  • where is the column name stored? Commented Sep 1, 2016 at 8:57
  • @Roysh - not according to angular docs Commented Sep 1, 2016 at 9:01
  • @user1187282 it looks that you're looking for loop in Angular to show your data Commented Sep 1, 2016 at 9:06

3 Answers 3

5

Yes, you can use a for...in loop to enumerate all the keys in an object as follows

var data = [
  {Name: "Matt", Job: "Teacher"},
  {Name: "Alice", Job: "Doctor"}];

data.forEach(function(e,i){
  console.log(i);
  for(var key in e){
      console.log(key + ':' + e[key] ); 
  }
});

Note that ive used Array.forEach, but angular.forEach will have much the same structure.

Sign up to request clarification or add additional context in comments.

2 Comments

you're the best !!
@user1187282 awww, thanks. (BTW, I think that for...in can also be done with angular.forEach - it supports iterating an object too as I discovered by reading the docs)
1

You may also try this:

data.forEach(function(item){
      Object.keys(item).forEach(function(key){
            console.log(key + ":" + item[key]);
      });
   });

Comments

-1

Try following two options: Generic JavaScript and AngularJs

//AngularJS for each 
var app = angular.module("app", []);
var values = [{'name': 'Jimi', 'gender': 'male'},{'name': 'Peter', 'gender': 'male'},{'name': 'Bob', 'gender': 'male'}];
app.controller('forEachController', ['$scope', function ($scope) {
        $scope.names = [];
        //Angular for each
        console.log("AngularJs Output");
        angular.forEach(values, function (value, key) {
            angular.forEach(value, function (val, k) {
              console.log(k + ":" + val);
            });
        });  
 }]);
console.log("JavaScript Output");
//Generic JavaScript for each
values.forEach(function(item){
  Object.keys(item).forEach(function(key){
    console.log(key + ":" + item[key]);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="forEachController"></div>

1 Comment

It looks that question is asking to show angular for each to iterate data, that's why I've used Angular, Please justify downvote ?

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.