suppose I have a json file and I would like to loop through the values like so:
假设我有一个json文件,我想像这样遍历值:
var myModel = {"id": 0, "date": "2014-10-28", "amount": 1111, "productId": "2", "description": "Cash"};
for (value in myModel)
{
//element(by.model(key)).clear().sendKeys(value);
}
This is a part of a jasmine script but it is not the point. The question is how can I loop through my model per key i.e 'id','date' etc and their values in angular js?
这是茉莉花脚本的一部分,但它不是重点。问题是如何在每个键上循环我的模型,即“id”,“date”等以及它们在角度js中的值?
2 个解决方案
#1
1
i think you need this:
我想你需要这个:
for (key in myModel) {
console.log("key is :",key)
console.log("value is:",myModel[key])
}
#2
1
Angular foreach will do the trick
角度的foreach将成功
Invokes the iterator function once for each item in obj collection, which can be either an object or an array. The iterator function is invoked with iterator(value, key, obj), where value is the value of an object property or an array element, key is the object property key or array element index and obj is the obj itself. Specifying a context for the function is optional.
为obj集合中的每个项目调用迭代器函数一次,可以是对象或数组。使用iterator(value,key,obj)调用迭代器函数,其中value是对象属性或数组元素的值,key是对象属性键或数组元素索引,obj是obj本身。指定函数的上下文是可选的。
var values = {"id": 0, "date": "2014-10-28", "amount": 1111, "productId": "2",
"description": "Cash"};
var log = [];
angular.forEach(values, function(value, key) {
this.push(key + ': ' + value);
}, log);
console.log(log);
#1
1
i think you need this:
我想你需要这个:
for (key in myModel) {
console.log("key is :",key)
console.log("value is:",myModel[key])
}
#2
1
Angular foreach will do the trick
角度的foreach将成功
Invokes the iterator function once for each item in obj collection, which can be either an object or an array. The iterator function is invoked with iterator(value, key, obj), where value is the value of an object property or an array element, key is the object property key or array element index and obj is the obj itself. Specifying a context for the function is optional.
为obj集合中的每个项目调用迭代器函数一次,可以是对象或数组。使用iterator(value,key,obj)调用迭代器函数,其中value是对象属性或数组元素的值,key是对象属性键或数组元素索引,obj是obj本身。指定函数的上下文是可选的。
var values = {"id": 0, "date": "2014-10-28", "amount": 1111, "productId": "2",
"description": "Cash"};
var log = [];
angular.forEach(values, function(value, key) {
this.push(key + ': ' + value);
}, log);
console.log(log);