我的问题是 SCORES,当我 console.log 整个对象时,分数部分以某种方式显示了一组额外的括号。
$(".submit").on("click", function() {
var newRoommate = {
"firstname": $('#first_name').val().trim(),
"lastname": $('#last_name').val().trim(),
"photo": $('#user_photo').val().trim(),
scores: [
$('#question_1').val(),
$('#question_2').val(),
$('#question_3').val(),
$('#question_4').val(),
$('#question_5').val(),
$('#question_6').val(),
$('#question_7').val(),
$('#question_8').val(),
$('#question_9').val(),
$('#question_10').val()
],
scoresInput
};
console.log(newRoommate); // .... scores[]: Array(10)
为什么显示scores[]:?
理想情况下,我希望它在 console.log 时产生类似这样的结果。
{
firstname:"Ricky",
lastname:"Bobby",
photo:"http://www.topcelebsjackets.com/wp-content/uploads/2017/03/Talladega-Nights-Wonder-Bread-Ricky-Bobby-Jacket-6-570x708.jpg",
scores:[
5,
1,
4,
4,
5,
1,
2,
5,
4,
1
]
}
最佳答案
Chrome、Firefox、NodeJS 等对于相同的 console.log
语句会有不同的结果。
那是因为 console.log
不是 JS 传递的函数,而是 JS 运行的每个环境实际传递不同的实现。如果您使用原始 JS(例如 Chrome V8),您会注意到 console
甚至没有定义(其中包括 document
或 window
)
在你的例子中,scores[]: Array(10)
实际上是说 你有一个 scores
键,它是一个数组,有 10元素。
相关: Throwing custom error doesn't preserve Error formatting in the log
关于javascript - 数组添加额外的方括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48296671/