Answer the question
In order to leave comments, you need to log in
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
}
};
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
Object
.values(moto)
.filter(item => item.color === 'Красный' && item.MaxSpeed >= 50)
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/
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 questionAsk a Question
731 491 924 answers to any question