我有这样的代码,需要为现有代码使用for循环,而不是显示1,2,3等代码在我尝试使用不同类型但获取任何数据的javascript文件中
$rootScope.AgeM = [
{M: '1'},
{M: '2'},
{M: '3'},
{M: '4'},
{M: '5'},
{M: '6'},
{M: '7'},
{M: '8'},
{M: '9'},
{M: '10'},
{M: '11'},
{M: '12'},
]
for在数组中。重复相同的数据或输入1到12之间的数字感到有点困难
答案 0 :(得分:1)
您需要使用Object.keys
function getAge() {
let AgeM = [
{M: '1'},
{M: '2'},
{M: '3'},
{M: '4'},
{M: '5'},
{M: '6'},
{M: '7'},
{M: '8'},
{M: '9'},
{M: '10'},
{M: '11'},
{M: '12'},
]
let age;
let index = 0;
Object.keys(AgeM).forEach(function (key) {
age = AgeM[key];
console.log('for index ', index + ' age is :' + age.M);
index++;
});
}