4 回答
TA贡献1772条经验 获得超5个赞
const map = new Map();
const cars = new Array("Porshe","Mercedes");
const bikes = new Array("Yamaha","Mitsubishi");
map.set('cars', cars);
map.set('bikes', bikes);
您可以像这样检索它们:
for(let arrayName of map.keys()) {
console.log(arrayName);
for(let item of map.get(arrayName)) {
console.log(item);
}
}
输出:
cars
Porshe
Mercedes
bikes
Yamaha
Mitsubishi
TA贡献1934条经验 获得超2个赞
for (let i=0;i<vehicles.length;i++){
for(let j = 0; j< vehicles[i].length;j++){
Console.log(vehicles[i][j]);
}
}
变量名没有用,当您尝试访问数据时......只有对象存储在数组中,而不是每个变量名。
TA贡献1883条经验 获得超3个赞
这是行不通的,另一个数组中的数组不是属性,因此没有 propertyName。你想要做的是创建一个像这样的对象:
arr1 = [value1, value2];
arr2 = [value1, value2];
obj = { 'a1': arr1, 'a2': arr2}
然后你可以迭代对象键,因为现在它是一个对象:
Object.keys(obj).forEach(key => console.log(key + ' = '+ obj[key]);
TA贡献1891条经验 获得超3个赞
干得好。
var vehicles={cars: ["Porshe","Mercedes"], bikes: ["Yamaha","Mitsubishi"]};
for( var vehicle in vehicles){ console.log(vehicle)} // this returns the keys in object i.e. "cars" and "bikes" not the values of array
for( var mark in vehicle){ console.log(mark) // this will loop on "bikes" and "cars"
要获得您需要做的值。
for(var type in vehicles) { // will give type of vehicle i.e. "cars" and "bikes"
vehicles[type].forEach(vehicle => { // will get values for each type and loop over them
console.log(vehicle); // this will print the values for every car and bike
)};
}
添加回答
举报