[英]Can not get any value using for each loop of angular.js
I have a problem in angular.js foreach loop.I have a console message console.log('course data',response.data);
我在angular.js foreach循环中遇到问题。我有一个控制台消息
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. 我有一些角度的foreach循环的代码,如下所示。
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.
为obj集合中的每个项目调用一次迭代器函数,该集合可以是对象也可以是数组。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.