0

I have a problem in angular.js foreach loop.I have a console message console.log('course data',response.data); whose output given below.

course data [Object, Object, Object]
0: Objectcourse_name: "Master of computer 
1: Objectcourse_name: "Bachelor of Technology"
2: Objectcourse_name: "Master in Technology"
length: 3__proto__: Array[0]

i have some code for angular foreach loop which is given below.

angular.forEach(response.data, function(value, key){
       console.log(key + ': ' + value);
     });

but here i am getting the following output of the console message given inside the code.

0: [object Object]
1: [object Object]
2: [object Object]

I am not getting the key name as well as value.Please help me to resolve this issue.

5 Answers 5

0

try this

angular.forEach(response.data, function(obj){
   console.log(obj.name + ': ' + obj.value);  });

Here name and value will be the key of your Object contains..

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

Comments

0

I think your data is an array of objects. Try:

angular.forEach(response.data, function(index, item){
   console.log(index + ': ' + item.Objectcourse_name);
});

Comments

0

I found myself also the answer.i tried the below code and got the output.

angular.forEach(response.data, function(value){
       console.log(value.course_name)
    });

Comments

0

Your Code absolutely fine, but the problem is with your array data. " is missing against Master of Computer. I have describe you another example below.

course data [Object, Object, Object]
0: Objectcourse_name: "Master of computer" 
1: Objectcourse_name: "Bachelor of Technology"
2: Objectcourse_name: "Master in Technology"
length: 3__proto__: Array[0]

angular.forEach

Invokes the iterator function once for each item in obj collection, which can be either an object or an array.

var obj = {name: 'misko', gender: 'male'};
var log = [];
angular.forEach(obj, function(value, key) {
  console.log(key + ': ' + value);
});
// it will log two iteration like this
// name: misko
// gender: male

See complete documentation here

Need any clarifications, please see its answer

Comments

-1

You are concatenating strings, not printing an object. Try this:

console.log(key, value)

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.