0

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.

4
  • 1
    You are storing arrays in array. They will not have any name, object properties have names not arrays. If you want names then do something like var vehicles={cars: ["Porshe","Mercedes"], bikes: ["Yamaha","Mitsubishi"]} Commented Oct 18, 2019 at 7:33
  • Ok, so now i am albe to get the array names but when i try to do loop over the elements inside it's giving me number of letters inside the name of the array like: var vehicles={cars: ["Porshe","Mercedes"], bikes: ["Yamaha","Mitsubishi"]} for( var vehicle in vehicles){ console.log(vehicle) //That is working fine now for( var mark in vehicle){ console.log(mark) //That's returning lenght of the name of the array like 0,1,2,3,4,5 for Porshe } } Commented Oct 18, 2019 at 8:00
  • You can do. vehicles["cars"].forEach(item => console.log(item)) in place of "cars" you can place your variable. Commented Oct 18, 2019 at 8:03
  • I've posted the answer. Commented Oct 18, 2019 at 9:09

4 Answers 4

1
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
Sign up to request clarification or add additional context in comments.

Comments

0
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.

Comments

0

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]);

Comments

0

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
    )};
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.