My JSON object is displaying differently when I alert it:
https://i.stack.imgur.com/lwNIJ.png
I would like it to show like this instead:
https://i.stack.imgur.com/cVakX.png
function getEmployeeNameById(id){
return staffArray.find(item => item.id === id).name;
}
var staffArray = [
{"id":"135","name":"Jenny"},
{"id":"162","name":"Kelly"}
];
$("#submit").click(function (e) {
var mistakes = {};
$(".validation").each(function(){
var empl_id = $(this).attr('id').replace(/[^\d]/g, '');
var e_name = getEmployeeNameById(empl_id);
if(!mistakes[e_name]) mistakes[e_name] = [];
if ( $(this).val() == "" ) {
mistakes[e_name].push( $(this).attr('id').replace(/[^a-zA-Z]/g, '') + " must be filled!");
//errors[w_name].push("second number must be smaller than first");
}
if ( $(this).attr('id') == "second-"+empl_id && ($(this).val() > $('#first-'+empl_id+'').val())) {
mistakes[e_name].push("second number must be smaller than first");
}
});
alert(JSON.stringify(mistakes, null, 2));
e.preventDefault();
e.stopPropagation();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="post">
First<input id="first-135" class="validation" name="first" type="text" value="5"><br>
Second<input id="second-135" class="validation" name="second" type="text" value="8"><br>
Signature<input id="signature-135" class="validation" name="signature" type="text"><br>
<input id="submit" type="submit" value="Submit">
</form>