B
B
Besso172018-02-13 16:55:13
JavaScript
Besso17, 2018-02-13 16:55:13

How to display an object?

I have a motorcycle object, it has 7 motorcycles and each has its own characteristics.

var moto = {
  motorcycle1: {
    color: "Черный",
    model: "Suzuki",
    MaxSpeed: 120
  },
  motorcycle2: {
    color: "Красный",
    model: "Honda",
    MaxSpeed: 125
  },
  motorcycle3: {
    color: "Белый",
    model: "BMW",
    MaxSpeed: 105
  },
  motorcycle4: {
    color: "Красный",
    model: "Ducati",
    MaxSpeed: 130
  },
  motorcycle5: {
    color: "Белый",
    model: "Kawasaki",
    MaxSpeed: 150
  },
  motorcycle6: {
    color: "Зеленый",
    model: "Урал",
    MaxSpeed: 90
  },
  motorcycle7: {
    color: "Черный",
    model: "Yamaha",
    MaxSpeed: 160
  }
};

I need to request the color and speed of a motorcycle and display those motorcycles in which the conditions match, namely the color and speed greater than or equal to the one I entered.
I did like this, everything works, but I don't like this approach. How else to do it, I can't think of. How can you do better?
var colorMoto = prompt("Введите цвет мотоцикла");
var speed = +prompt("Введите максимальную скорость");
for (var key1 in moto)
   for (var key2 in moto [key1]){
   		
   if ((moto [key1][key2]) == 160 && colorMoto == "Черный" && speed <= 160 ){
     	document.write (key1 + "<br>");
 		} 
 	else if ((moto [key1][key2]) == 120 && colorMoto == "Черный" && speed <= 120 ){
     	document.write (key1 + "<br>");
 		} 
 	else if ((moto [key1][key2]) == 90 && colorMoto == "Зеленый" && speed <= 90){
 		document.write (key1 + "<br>");
 		}
 	else if ((moto [key1][key2]) == 125 && colorMoto == "Красный" && speed <= 125){
 		document.write (key1 + "<br>");
 		}
 	else if ((moto [key1][key2]) == 130 && colorMoto == "Красный" && speed <= 130){
 		document.write (key1 + "<br>");
 		}
 	else if ((moto [key1][key2]) == 150 && colorMoto == "Белый" && speed <= 150){
 		document.write (key1 + "<br>");
 		}
 	else if ((moto [key1][key2]) == 105 && colorMoto == "Белый" && speed <= 105){
 		document.write (key1 + "<br>");
 		}
  	}

Answer the question

In order to leave comments, you need to log in

5 answer(s)
L
lemme, 2018-02-13
@Besso17

Object
  .values(moto)
  .filter(item => item.color === 'Красный' && item.MaxSpeed >= 50)

V
Vitaly the Wise, 2018-02-13
@tapinambur0508

Don't you want to try doing it in an array?

V
Vadim, 2018-02-13
@v1996v04v04

The simplest option (if I understand correctly what you want), of course, it is preferable to store objects in an array. https://jsfiddle.net/qrrvnbcy/6/

T
twobomb, 2018-02-13
@twobomb

Here is an example

B
Besso17, 2018-02-17
@Besso17

Did it anyway. As wanted.

var moto = {
  motorcycle1: {
    color: "Черный",
    model: "Suzuki",
    MaxSpeed: 120
  },
  motorcycle2: {
    color: "Красный",
    model: "Honda",
    MaxSpeed: 125
  },
  motorcycle3: {
    color: "Белый",
    model: "BMW",
    MaxSpeed: 105
  },
  motorcycle4: {
    color: "Красный",
    model: "Ducati",
    MaxSpeed: 130
  },
  motorcycle5: {
    color: "Белый",
    model: "Kawasaki",
    MaxSpeed: 150
  },
  motorcycle6: {
    color: "Зеленый",
    model: "Урал",
    MaxSpeed: 90
  },
  motorcycle7: {
    color: "Черный",
    model: "Yamaha",
    MaxSpeed: 160
  }
};

var color = prompt("Введите цвет мотоцикла");
var speed = +prompt("Введите максимальную скорость");


for (var key1 in moto)
  for(var key2 in moto [key1]){
    if(moto[key1].color == color && moto[key1].MaxSpeed >= speed){
      var a = key1;
      alert(a);
      break;
    }
  };

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question