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.
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..
I think your data is an array of objects. Try:
angular.forEach(response.data, function(index, item){
console.log(index + ': ' + item.Objectcourse_name);
});
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)
});
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
You are concatenating strings, not printing an object. Try this:
console.log(key, value)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.