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
)};
}
添加回答
舉報