Есть объект:
$sum = {
"document":{
"0.3":{"in_city":"1600","0_49":"2200","50_200":"3200","200_999":"4200"},
"0.5":{"in_city":"2000","0_49":"2400","50_200":"3400","200_999":"4400"},
"1":{"in_city":"2400","0_49":"2700","50_200":"3700","200_999":"4700"},
"1.5":{"in_city":"2600","0_49":"3000","50_200":"4000","200_999":"5000"},
"2":{"in_city":"2800","0_49":"3500","50_200":"4500","200_999":"5500"},
"2.5":{"in_city":"3000","0_49":"3800","50_200":"4800","200_999":"5800"},
"3":{"in_city":"3200","0_49":"4100","50_200":"5100","200_999":"6100"},
"3.5":{"in_city":"3350","0_49":"4300","50_200":"5400","200_999":"6400"},
"4":{"in_city":"3550","0_49":"4700","50_200":"5700","200_999":"6700"},
"4.5":{"in_city":"3750","0_49":"5000","50_200":"6000","200_999":"7000"},
"5":{"in_city":"3950","0_49":"5300","50_200":"6300","200_999":"7300"}
}
};
На сайте есть поле формы в которую можно написать число допустим "1.2" теперь как найти ближайший объект который больше числа 1.2 в объекте им является "1.5"
if( $weight in $sum.document ){
console.log( $sum.document[$weight] );
}
in не подходит так как он проверяет если такой же объект или нет
const $sum = {
"document":{
"0.3":{"in_city":"1600","0_49":"2200","50_200":"3200","200_999":"4200"},
"0.5":{"in_city":"2000","0_49":"2400","50_200":"3400","200_999":"4400"},
"1":{"in_city":"2400","0_49":"2700","50_200":"3700","200_999":"4700"},
"1.5":{"in_city":"2600","0_49":"3000","50_200":"4000","200_999":"5000"},
"2":{"in_city":"2800","0_49":"3500","50_200":"4500","200_999":"5500"},
"2.5":{"in_city":"3000","0_49":"3800","50_200":"4800","200_999":"5800"},
"3":{"in_city":"3200","0_49":"4100","50_200":"5100","200_999":"6100"},
"3.5":{"in_city":"3350","0_49":"4300","50_200":"5400","200_999":"6400"},
"4":{"in_city":"3550","0_49":"4700","50_200":"5700","200_999":"6700"},
"4.5":{"in_city":"3750","0_49":"5000","50_200":"6000","200_999":"7000"},
"5":{"in_city":"3950","0_49":"5300","50_200":"6300","200_999":"7300"}
},
}
const num = 1.2;
const item = Object.keys($sum.document).map(item => +item).sort().find(key => key > num);
console.log($sum.document[item])
$sum = {
"document":{
"0.3":{"in_city":"1600","0_49":"2200","50_200":"3200","200_999":"4200"},
"0.5":{"in_city":"2000","0_49":"2400","50_200":"3400","200_999":"4400"},
"1":{"in_city":"2400","0_49":"2700","50_200":"3700","200_999":"4700"},
"1.5":{"in_city":"2600","0_49":"3000","50_200":"4000","200_999":"5000"},
"2":{"in_city":"2800","0_49":"3500","50_200":"4500","200_999":"5500"},
"2.5":{"in_city":"3000","0_49":"3800","50_200":"4800","200_999":"5800"},
"3":{"in_city":"3200","0_49":"4100","50_200":"5100","200_999":"6100"},
"3.5":{"in_city":"3350","0_49":"4300","50_200":"5400","200_999":"6400"},
"4":{"in_city":"3550","0_49":"4700","50_200":"5700","200_999":"6700"},
"4.5":{"in_city":"3750","0_49":"5000","50_200":"6000","200_999":"7000"},
"5":{"in_city":"3950","0_49":"5300","50_200":"6300","200_999":"7300"}
}
};
$("#check").click(function(){
let need=$("#need").val();
//Сортируем ключи
let keys=Object.keys($sum.document).sort(function(a, b){
return parseFloat(a) - parseFloat(b);
});
//Ищем большее значение и выходим из цикла
for(let i in keys){
if(need<keys[i]){
console.log(keys[i]+":"+JSON.stringify($sum.document[keys[i]]));
break;
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" id="need">
<button id="check">Получить ближайшеее значение</button>
Виртуальный выделенный сервер (VDS) становится отличным выбором
Использую библиотеку Spring BootСверстал элементарную html-страницу(не смотрите на этот код, знаю, он плох, я никогда не верстал да и вряд ли буду,...
есть два абзаца, условно A и B, только при нажатии на верхний блок, на сам абзац он меняется местами с нижним, то есть кликабельный только верхнийвсегда...
Почему код не срабатывает, хотя он написан с соблюдением разных стандартов (ECMAScript) ? Вот код: