Scratchbook

Das Leben ist immer anders als die Realität.

print_r für Javascript

Claude, 11. Juli 2009, 14:29 Uhr

Warning: Use of undefined constant ri_rand_compare - assumed 'ri_rand_compare' (this will throw an Error in a future version of PHP) in /home/httpd/vhosts/scratchbook.ch/httpdocs/wp-content/plugins/random-image-widget/random_image.php on line 129 Warning: Use of undefined constant ri_rand_compare - assumed 'ri_rand_compare' (this will throw an Error in a future version of PHP) in /home/httpd/vhosts/scratchbook.ch/httpdocs/wp-content/plugins/random-image-widget/random_image.php on line 130 Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/httpd/vhosts/scratchbook.ch/httpdocs/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384 Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/httpd/vhosts/scratchbook.ch/httpdocs/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384 Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/httpd/vhosts/scratchbook.ch/httpdocs/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384 Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/httpd/vhosts/scratchbook.ch/httpdocs/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384 Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/httpd/vhosts/scratchbook.ch/httpdocs/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384 Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/httpd/vhosts/scratchbook.ch/httpdocs/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384 Warning: preg_match(): Compilation failed: invalid range in character class at offset 4 in /home/httpd/vhosts/scratchbook.ch/httpdocs/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 1384 Warning: preg_match_all(): Compilation failed: invalid range in character class at offset 4 in /home/httpd/vhosts/scratchbook.ch/httpdocs/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 700 Warning: Invalid argument supplied for foreach() in /home/httpd/vhosts/scratchbook.ch/httpdocs/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 707 Warning: preg_match_all(): Compilation failed: invalid range in character class at offset 4 in /home/httpd/vhosts/scratchbook.ch/httpdocs/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 700 Warning: Invalid argument supplied for foreach() in /home/httpd/vhosts/scratchbook.ch/httpdocs/wp-content/plugins/lightbox-plus/classes/shd.class.php on line 707

Die Funktion, die ich bei Javascript am meisten vermisst habe, ist das PHP-Equivalent „print_r“ – einfach mal den Inhalt von Objekten schön darstellen.

Es gibt im Netz schon eine Version – aber deren Ausgabe sieht nicht genau so aus wie bei PHP. Deshalb hier meine Version mit kleinen Modifikationen:

/**
 * PHP. Javascript. Print_r. Nice. Object. Dumper.
 * Original. Code: http://www.openjs.com/scripts/others/dump_function_php_print_r.php
 * Modified. By. Claude. Hohl. Namics.
 */
 
function print_r(arr, level) {
 
	var dumped_text = "";
	if (!level) level = 0;
 
	//The padding given at the beginning of the line.
	var level_padding = "";
	var bracket_level_padding = "";
 
	for (var j = 0; j < level + 1; j++) level_padding += "    ";
	for (var b = 0; b < level; b++) bracket_level_padding += "    ";
 
	if (typeof(arr) == 'object') { //Array/Hashes/Objects 
		dumped_text += "Array\n";
		dumped_text += bracket_level_padding + "(\n";
		for (var item in arr) {
 
			var value = arr[item];
 
			if (typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "[" + item + "] => ";
				dumped_text += print_r(value, level + 2);
			} else {
				dumped_text += level_padding + "[" + item + "] => " + value + "\n";
			}
 
		}
		dumped_text += bracket_level_padding + ")\n\n";
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>" + arr + "<===(" + typeof(arr) + ")";
	}
 
	return dumped_text;
 
}

Testen (irgendwo in ein HTML einbetten):

var obj = {
	wert1: 'pfu',
	wert2: 'bla',
	schachtel: {
		foo: 'bar'
	},
	nummer: 2,
	und: 'so weiter'
}
 
alert(print_r(obj));

Ausgabe:

Array
(
    [wert1] => pfu
    [wert2] => bla
    [schachtel] => Array
        (
            [foo] => bar
        )
 
    [nummer] => 2
    [und] => so weiter
)
Warning: count(): Parameter must be an array or an object that implements Countable in /home/httpd/vhosts/scratchbook.ch/httpdocs/wp-content/themes/scratchbook/navigation_bar_bottom.php on line 1