ฉันจะดูโครงสร้างของอาร์เรย์ใน JavaScript โดยใช้alert()อย่างไร
console.debugจะทำงานได้ดีขึ้นจริง
ฉันจะดูโครงสร้างของอาร์เรย์ใน JavaScript โดยใช้alert()อย่างไร
console.debugจะทำงานได้ดีขึ้นจริง
คำตอบ:
แนวทางพื้นฐานคือalert(arrayObj.join('\n'))ซึ่งจะแสดงองค์ประกอบอาร์เรย์แต่ละรายการในแถว
alert(myArray.join('\n'));
แก้ไข: Firefox และ Google Chrome มีJSONออบเจ็กต์ในตัวแล้วดังนั้นคุณสามารถพูดได้alert(JSON.stringify(myArray))โดยไม่ต้องใช้ปลั๊กอิน jQuery นี่ไม่ใช่ส่วนหนึ่งของข้อมูลจำเพาะภาษา Javascript ดังนั้นคุณไม่ควรพึ่งพาJSONอ็อบเจ็กต์ที่มีอยู่ในเบราว์เซอร์ทั้งหมด แต่สำหรับวัตถุประสงค์ในการดีบักมันมีประโยชน์อย่างเหลือเชื่อ
ฉันมักจะใช้ปลั๊กอิน jQuery-jsonดังนี้:
alert( $.toJSON(myArray) );
สิ่งนี้จะพิมพ์อาร์เรย์ในรูปแบบเช่น
[5, 6, 7, 11]
อย่างไรก็ตามสำหรับการดีบักโค้ด Javascript ของคุณฉันขอแนะนำอย่างยิ่งFirebug มันมาพร้อมกับคอนโซล Javascript ดังนั้นคุณสามารถพิมพ์โค้ด Javascript สำหรับหน้าใดก็ได้และดูผลลัพธ์ สิ่งต่างๆเช่นอาร์เรย์ได้รับการพิมพ์ในรูปแบบที่มนุษย์อ่านได้ซึ่งใช้ด้านบนแล้ว
Firebug ยังมีดีบักเกอร์เช่นเดียวกับหน้าจอสำหรับช่วยคุณดูและแก้ไขข้อบกพร่อง HTML และ CSS ของคุณ
ส่งอาร์เรย์ js ของคุณไปยังฟังก์ชันด้านล่างและจะทำเช่นเดียวกับฟังก์ชัน php print_r ()
alert(print_r(your array)); //call it like this
function print_r(arr,level) {
var dumped_text = "";
if(!level) level = 0;
//The padding given at the beginning of the line.
var level_padding = "";
for(var j=0;j<level+1;j++) level_padding += " ";
if(typeof(arr) == 'object') { //Array/Hashes/Objects
for(var item in arr) {
var value = arr[item];
if(typeof(value) == 'object') { //If it is an array,
dumped_text += level_padding + "'" + item + "' ...\n";
dumped_text += print_r(value,level+1);
} else {
dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
}
}
} else { //Stings/Chars/Numbers etc.
dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
}
return dumped_text;
}
คุณสามารถใช้ได้ alert(arrayObj.toSource());
ฉันขอแนะนำให้ใช้ toString ()
เช่น alert(array.toString()), หรือconsole.log(array.toString())
หากสิ่งที่คุณต้องการคือการแจ้งเตือน () เนื้อหาของอาร์เรย์ของวัตถุฉันขอแนะนำให้คุณกำหนดวิธีการ toString () ในวัตถุด้วยการแจ้งเตือนอย่างง่าย (MyArray) เนื้อหาทั้งหมดของอาร์เรย์จะแสดงในการแจ้งเตือน
นี่คือตัวอย่าง:
//-------------------------------------------------------------------
// Defininf the Point object
function Point(CoordenadaX, CoordenadaY) {
// Sets the point coordinates depending on the parameters defined
switch (arguments.length) {
case 0:
this.x = null;
this.y = null;
break;
case 1:
this.x = CoordenadaX;
this.y = null;
break;
case 2:
this.x = CoordenadaX;
this.y = CoordenadaY;
break;
}
// This adds the toString Method to the point object so the
// point can be printed using alert();
this.toString = function() {
return " (" + this.x + "," + this.y + ") ";
};
}
จากนั้นถ้าคุณมีอาร์เรย์ของจุด:
var MyArray = [];
MyArray.push ( new Point(5,6) );
MyArray.push ( new Point(7,9) );
คุณสามารถพิมพ์โทร:
alert(MyArray);
หวังว่านี่จะช่วยได้!
คุณสามารถเขียนฟังก์ชันที่จะแปลงและจัดรูปแบบอาร์เรย์นี้เป็นสตริง ดียิ่งขึ้น: ใช้FireBugในการดีบักแทนการแจ้งเตือน
ใช้ Firebug (คอนโซล Chrome ฯลฯ ) ดีกว่าและใช้ console.dir ()
เพื่อจุดประสงค์ในการอ่านคุณสามารถใช้:
alert(JSON.stringify(someArrayOrObj, '', 2));
เพิ่มเติมเกี่ยวกับJSON.stringify ()
ตัวอย่าง:
let user = {
name: "John",
age: 30,
roles: {
isAdmin: false,
isEditor: true
}
};
alert(JSON.stringify(user, "", 2));
/* Result:
{
"name": "John",
"age": 30,
"roles": {
"isAdmin": false,
"isEditor": true
}
}
*/
alert($("#form_id").serialize());
javascriptซึ่งระบุว่า "หากไม่รวมแท็กสำหรับเฟรมเวิร์ก / ไลบรารีด้วยคาดว่าจะมีคำตอบ JavaScript ที่แท้จริง" แม้ว่าจะไม่เป็นเช่นนั้นก็ตาม ... คำถามคือถามเกี่ยวกับอาร์เรย์ไม่ใช่รูปแบบ HTML
console.log- เหมาะสำหรับการวิเคราะห์วัตถุ JavaScript