0

I have a JSON object and when i alert it i get this:

enter image description here

and i want to get this:

enter image description here

function getNameById(id){
    return usersArray.find(item => item.id === id).name;
}

var usersArray = [
          {"id":"135","name":"Jenny"},
                    {"id":"162","name":"Kelly"}
                 ];
$("#submit").click(function (e) {           
    var errors = {};
    
    $(".validation").each(function(){
       var worker_id = $(this).attr('id').replace(/[^\d]/g, '');
       var w_name = getNameById(worker_id);
       if(!errors[w_name]) errors[w_name] = [];
       if ( $(this).val() == "" ) {
           errors[w_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-"+worker_id  && ($(this).val() > $('#first-'+worker_id+'').val())) {
         errors[w_name].push("second number must be smaller than first");
       }     
    });
    
    alert(JSON.stringify(errors, 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>
How can i achieve that?
lewis4u
  • 14,256
  • 18
  • 107
  • 148

3 Answers3

2

Transform your object to a string like this

let obj = {
  "Jenny" : [
    "Second number must be smaller than first",
    "Signature must be filled !"
  ]
};

let str = "";
Object.keys(obj).forEach(k => {
  str += k + ":\n";
  str += obj[k].join(",\n");
});

console.log(str);
Weedoze
  • 13,683
  • 1
  • 33
  • 63
1

Extract the data from the JSON data that you have in errors instead of running JSON.stringify directly. You should be able to get the data like this: errors["Jenny"] to get a list of the errors. Then combine them into a string according to your liking.

gideonite
  • 1,211
  • 1
  • 8
  • 13
0

I honestly don't think your question has absolutely anything to do with JSON. The only reason why some JSON even shows up is because you're generating it for the alert():

alert(JSON.stringify(errors, null, 2));
   // ^^^^^^^^^^^^^^ This generates JSON

If you want to concatenate some array items you can use a combination of the concatenation operator (+) and Array.join():

alert(w_name + ":\n" + errors[w_name].join(",\n"));

Tweak format to your liking.

var w_name = "Jenny";
var errors = {};

errors[w_name] = [];
errors[w_name].push("Second number must be smaller than first");
errors[w_name].push("Signature must be filled!");

alert(w_name + ":\n" + errors[w_name].join(",\n"));
Álvaro González
  • 142,137
  • 41
  • 261
  • 360