Angular forEach - 为 obj 集合中的每个项目调用一次迭代器函数,它可以是对象或数组。
var values = {name: 'misko', gender: 'male'};
angular.forEach(values, function(value, key) {
console.log(key + ': ' + value);
});
// Output:
// "name: misko"
// "gender: male"
for..in - 以任意顺序迭代对象的enumerable properties。对于每个不同的属性,可以执行语句。
var obj = {a:1, b:2, c:3};
for (var prop in obj) {
console.log("obj." + prop + " = " + obj[prop]);
}
// Output:
// "obj.a = 1"
// "obj.b = 2"
// "obj.c = 3"
forEach - 方法对每个数组元素执行一次提供的函数。
// Notice that index 2 is skipped since there is no item at
// that position in the array.
[2, 5, , 9].forEach(function (element, index, array) {
console.log('a[' + index + '] = ' + element);
});
// logs:
// a[0] = 2
// a[1] = 5
// a[3] = 9
就性能而言,这取决于您正在使用的数据结构,如果是Array,我建议使用Angular.forEach or native forEach,如果是Object,for..in 将是最好的,但是看起来Angular.forEach 也能很好地处理对象。取决于您使用的数据量。如果它很大,我建议你使用像 Lodash or Underscore 这样的库,它们可以很好地处理数据。