Reputation: 17
I am trying to access name of the array inside of other array in the loop but i am failing. I am able to access name of that array if it's not inside another array by fx. Object.keys({thisObject})[0] but when it's inside of another array it does not work.
I have already tried for each loops, for loops. Nesting initialized arrays in the new array:
var arr1 = [1,2,3]
var arr2 = [4,5,6]
var arr3 = new Array(arr1,arr2)
Still, in the loop i cannot get the name of the arr1 and arr2. I have possibility to access their values but not the names..
var cars = new Array("Porshe","Mercedes");
var bikes = new Array("Yamaha","Mitsubishi");
var vehicles = new Array(cars, bikes);
for (var key in vehicles) {
var value = vehicles[key];// This is retruning whole array, not the name
console.log(Object.keys({vehicles[key]})[0]) // That does not work
vehicles[key].forEach(car => {
console.log(car)
});
}
//or
for (let i=0;i<vehicles.length;i++){
console.log(vehicles[i]); //This is also returning whole array - same method.
for(let j = 0; j< vehicles[i].car.length;j++){
console.log(vehicles[i][j]);
}
}
The result which i want to get is to listing the cars in the table where Cars are the headline and underneath are the Porsche, Mitsubishi and then Bikes are the next one.
Upvotes: 0
Views: 1399
Reputation: 208
Here you go.
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"
To get values you need to do.
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
)};
}
Upvotes: 0
Reputation: 11
const map = new Map();
const cars = new Array("Porshe","Mercedes");
const bikes = new Array("Yamaha","Mitsubishi");
map.set('cars', cars);
map.set('bikes', bikes);
You can retrieve them like this:
for(let arrayName of map.keys()) {
console.log(arrayName);
for(let item of map.get(arrayName)) {
console.log(item);
}
}
Output:
cars
Porshe
Mercedes
bikes
Yamaha
Mitsubishi
Upvotes: 1
Reputation: 337
that's not going to work, the arrays inside the other array are not properties, and thus have no propertyName. what you want to do, is create an object like so:
arr1 = [value1, value2];
arr2 = [value1, value2];
obj = { 'a1': arr1, 'a2': arr2}
then you can iterate over the object keys, because now it is an object:
Object.keys(obj).forEach(key => console.log(key + ' = '+ obj[key]);
Upvotes: 0
Reputation: 59
for (let i=0;i<vehicles.length;i++){
for(let j = 0; j< vehicles[i].length;j++){
Console.log(vehicles[i][j]);
}
}
The variablenames are of no use, when you try to access data... only the object is stored within the array, not the variablenames per say.
Upvotes: 0