简体   繁体   English

美化JSON对象警报

[英]Prettify JSON object alert

I have a JSON object and when i alert it i get this: 我有一个JSON对象,当我对其发出警报时,我得到了:

在此处输入图片说明

and i want to get this: 我想得到这个:

在此处输入图片说明

 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? 我该如何实现?

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); 

Extract the data from the JSON data that you have in errors instead of running JSON.stringify directly. errors的JSON数据中提取数据,而不是直接运行JSON.stringify You should be able to get the data like this: errors["Jenny"] to get a list of the errors. 您应该能够获得如下数据: errors["Jenny"]以获取错误列表。 Then combine them into a string according to your liking. 然后根据自己的喜好将它们组合成一个字符串。

I honestly don't think your question has absolutely anything to do with JSON. 老实说,我不认为您的问题与JSON有任何关系。 The only reason why some JSON even shows up is because you're generating it for the alert() : 甚至出现一些JSON的唯一原因是因为您是为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() : 如果要串联某些数组项,可以使用串联运算符( +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")); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM