我有一个像这样的数据数组,
tourGuide: [
{"location": "london"},
{"location": "paris"},
{"location": "frankfurt"}
]
我想重写tourGuide
,以便它也包含索引。所以它会变成这样:
tourGuide: [
{"location": "london", "index":0},
{"location": "paris", "index":1},
{"location": "frankfurt","index":2}
]
我尝试过这个:
var x, l=0;
for(x in tourGuide){
tourGuide["index"] = l;
l++;
}
但这可能不是正确的方法。
最佳答案
var x, l=0;
for(x in tourGuide){
tourGuide[x]["index"] = l;
l++;
}
关于javascript - 向数组添加一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10645895/