gpt4 book ai didi

javascript - 美化 JSON 对象警报

转载 作者:行者123 更新时间:2023-11-28 18:05:09 30 4
gpt4 key购买 nike

我有一个 JSON 对象,当我提醒它时,我得到这个:

enter image description here

我想得到这个:

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>
我怎样才能实现这一目标?

最佳答案

将你的对象转换为这样的字符串

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

关于javascript - 美化 JSON 对象警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42830381/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com