0

The variables following are defined through a Car prototype:

var Car = function(maker, type, model) {
  this.maker = maker;
  this.type = type;
  this.model = model;
}

var golf = new Car('VW', 'Hatchback', 'Golf');
var sentra = new Car('Nissan', 'Sedan', 'Sentra');
var _328i = new Car('BMW', 'Convertible', "328i");
var gallardo = new Car('Lamborghini', 'Convertible', "Gallardo");
var corniche = new Car('Rolls Royce', 'Sedan', "Corniche");

Car.prototype.year = 0;

golf.year = 2015;
sentra.year = 2010;
_328i.year = 2019;
gallardo.year = 2020;
corniche.year = 1998;  

How does one build an array of the var values for the property maker

Something that might console log like this:

(5) ['VW', 'Nissan', 'BMW', 'Lamborghini', 'Roll Royce']

3 Answers 3

1

You could create an array of the car instances and use the map function

console.log(
  [golf, sentra, _328i, gallardo, corniche].map((car) => car.maker)
)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your reply. As another commentator said, "There are a lot of solutions to that." Yours is interesting. I was thinking one could find the variables; golf, sentra etc. referencing the Car prototype and then the property 'maker'. You seem to manually entered the var names. If there were a hundred this might be burdensome. Any thoughts? Thanks
Raphael had my thought in mind. // Solution here const makersList = Array.from([golf, sentra, _328i, gallardo, corniche], car => car.maker ); // should print an array of makers string names console.log(makersList);
0

There are a lot of solutions to that. First, you need to make an array with all your cars, and then use a method to iterate on each car to get the car maker.

Here two examples:

var Car = function (maker, type, model) {
  this.maker = maker;
  this.type = type;
  this.model = model;
}

Car.prototype.year = 0;

var golf = new Car ('VW', 'Hatchback', 'Golf');
var sentra = new Car ('Nissan', 'Sedan', 'Sentra');
var _328i = new Car ('BMW', 'Convertible', "328i");
var gallardo = new Car ('Lamborghini', 'Convertible', "Gallardo", 2020);
var corniche = new Car('Rolls Royce', 'Sedan', "Corniche");

golf.year = 2015;
sentra.year = 2010;
_328i.year = 2019;
gallardo.year = 2020;
corniche.year = 1998;

var cars = [golf, sentra, _328i, gallardo, corniche]
var carsMakers = []

for(var i = 0; i < cars.length; i++) {
  var c = cars[i]
  carsMakers.push(c.maker)
}
console.log(carsMakers)

var carsMakers2 = cars.map(function(c) { return c.maker });
console.log(carsMakers2)

Comments

0

Just like the map approach but using Array.prototype.from:


var Car = function (maker, type, model) {
this.maker = maker;
this.type = type;
this.model = model;
}
var golf = new Car ('VW', 'Hatchback', 'Golf');
var sentra = new Car ('Nissan', 'Sedan', 'Sentra');
var _328i = new Car ('BMW', 'Convertible', "328i");
var gallardo = new Car ('Lamborghini', 'Convertible', "Gallardo");
var corniche = new Car('Rolls Royce', 'Sedan', "Corniche");

Car.prototype.year = 0;

golf.year = 2015;
sentra.year = 2010;
_328i.year = 2019;
gallardo.year = 2020;
corniche.year = 1998;

// Solution here
const makersList = Array.from([golf, sentra, _328i, gallardo, corniche], car => car.maker );

// should print an array of makers string names
console.log(makersList);

6 Comments

How is creating an array and using Array.from() and it's map function more performant than directly using the map function on the array? Array.from() is redundant here
Just one more way of doing the same thing. Will fix the performance allegation. If its redundant or not its up to the person asking :)
Thanks Raphael. Exactly.
Exactly, er, or maybe not. I thought you pulled the var names but in fact you entered them (?). Can you not pull the var names through the Car prototype reference (terminology might be wrong). Thanks
Then this is actually what you're looking for: stackoverflow.com/questions/19526456/…
|

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.