I have 2 questions based on the example below.
1- How can I get the closest 'jsonData.te' value to today?
2- How can I do the for loop in reverse without breaking this structure? (for example, it will look at key: 27 first, it will end when it finds the value of te there)
Thanks..
var demo = document.getElementsByName("test");
var today = moment().format('YYYY-MM-DD');
for (const dem of demo) {
const jsonObject = dem.getAttribute("value");
const jsonData = JSON.parse(jsonObject);
console.log(jsonData.te);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="hidden" name="test" value='{"key": 21, "sd": "2021-05-22", "te": "de1"}'>
<input type="hidden" name="test" value='{"key": 22, "sd": "2021-05-21", "te": "de2"}'>
<input type="hidden" name="test" value='{"key": 23, "sd": "2021-05-20", "te": "de3"}'>
<input type="hidden" name="test" value='{"key": 24, "sd": "2021-05-23", "te": "de4"}'>
<input type="hidden" name="test" value='{"key": 25, "sd": "2021-05-25", "te": "de5"}'>
<input type="hidden" name="test" value='{"key": 26, "sd": "2021-05-24", "te": "de6"}'>
<input type="hidden" name="test" value='{"key": 27, "sd": "2021-05-26", "te": "de7"}'>