1

I've got values inside array objects and supposed to populate in readable format but stuck as output gets error like "[object Object] undefined".

my desired output is supposed to be like:

Saab - Smodel1,Smodel2,Smodel3, Volvo - Vmodel1,VmodeL2,Vmodel3, BMW - Bmodel1,Bmodel2,Bmodel3,

here is my code:

const cars = [{
  "Saab":["Smodel1", "Smodel2", "Smodel3"],
  "Volvo":["Vmodel1", "Vmodel2", "Vmodel3"],
  "BMW":["Bmodel1", "Bmodel2", "Bmodel3"]
}];
let car_model = '';

for(let i = 0; i < cars.length; i++) {
car_model += cars[i] + "-" + cars[i][i] + "<br/>";
}

added for reference

 var cars = 
            var car, hash;
            for (var model in cars) {
                car = key;
                hash = cars[key];
                get(car, hash);
                seen(car, hash);
                (function loop(car, hash) {
                    setTimeout(function () {
                        get(car, hash);
                        loop(car, hash);
                    
                    }, 1000);
                })(car, hash);
            }

Current output: [object Object] undefined

mickmackusa
  • 43,625
  • 12
  • 83
  • 136

4 Answers4

0

You can loop over the only single object in your array using Object.entries(...) and then Array.map(...) the values to the desired strings. String.join(...) will also be of great help, as it allows you to easily join an array to a single string, with a given seperator.

const cars = [{
 "Saab":["Smodel1", "Smodel2", "Smodel3"],
  "Volvo":["Vmodel1", "Vmodel2", "Vmodel3"],
  "BMW":["Bmodel1", "Bmodel2", "Bmodel3"]
}];

console.log(
  Object
    .entries(cars[0]) // take your first (and only) element and split it into key value pairs
    .map(([key, values]) => // map the key value pairs
      `${key} - ${values.join(",")}` // to the desired output format
    )
    .join(",") // join the mapped values by a ,
)
MauriceNino
  • 6,214
  • 1
  • 23
  • 60
  • well good work right there sir however i will go on the "for loop" kind of thingy. – saint thomas Jul 04 '22 at 09:07
  • @saintthomas If you have found your answer, please accept it. – MauriceNino Jul 04 '22 at 09:28
  • havent found it yet but im working on using jquery children() function. maybe populating it on a table with some php manipulation will do the trick. please see above post i have added some reference of my script that its a bit difficult in integrating your input – saint thomas Jul 04 '22 at 09:50
  • Your question has multiple answers. If you have another question, open another StackOverflow thread. This question has nothing to do with jQuery children, or anything else. – MauriceNino Jul 04 '22 at 09:52
  • yah what i have added there is just for reference for some scenario. its just the for loop what i need thank you – saint thomas Jul 04 '22 at 09:58
  • You have answers for your loop. Mark one as the answer. What you are doing right now is just disrespectful to anyone that offered up their time to help you out. – MauriceNino Jul 04 '22 at 10:02
0
const cars = {
 "Saab":["Smodel1", "Smodel2", "Smodel3"],
  "Volvo":["Vmodel1", "Vmodel2", "Vmodel3"],
  "BMW":["Bmodel1", "Bmodel2", "Bmodel3"]
};

car_models = '';
for (let i=0; i < Object.keys(cars).length; i ++) {
    models = cars[Object.keys(cars)[i]];
    car_models += Object.keys(cars)[i] + " " + models.join();
}

Outputs

Saab Smodel1,Smodel2,Smodel3Volvo Vmodel1,Vmodel2,Vmodel3BMW Bmodel1,Bmodel2,Bmodel3
Daniel Setréus
  • 552
  • 4
  • 16
0

If you want to use only traditional for-loops

const cars = [{
 "Saab":["Smodel1", "Smodel2", "Smodel3"],
  "Volvo":["Vmodel1", "Vmodel2", "Vmodel3"],
  "BMW":["Bmodel1", "Bmodel2", "Bmodel3"]
}];

let car_model = '';

for(let i = 0; i < cars.length; i++) {
  const brands = Object.keys(cars[i]);
  for (let j = 0; j < brands.length; j++) {
    const models = cars[i][brands[j]];
    for (let k = 0; k < models.length; k++) {
        car_model += brands[j] + "-" + models[k] + "<br/>";
    }
  }
}

console.log(car_model)
Ele
  • 33,468
  • 7
  • 37
  • 75
0

A simple one-liner using Object.entries and forEach:

const cars = [{
  "Saab":["Smodel1", "Smodel2", "Smodel3"],
  "Volvo":["Vmodel1", "Vmodel2", "Vmodel3"],
  "BMW":["Bmodel1", "Bmodel2", "Bmodel3"]
  }];

Object.entries(cars[0]).forEach(([key, value]) => document.write(`${key}: ${value}`,'<br>'));
JMP
  • 4,417
  • 17
  • 30
  • 41