JSFiddle - React, Tailwind, and code Playground
by Alexis López Espinoza
JavaScript
/**
* print_r() for JavaScript
*
* @author Alexis López
* @param Array|Object array
* @param Integer cont (optional)
*
* Prints multidimensional arrays and objects
*/
var arr = [
1, 2, 3,
[
[
[
[
{
a: 1,
b: 2,
c: {
d: 3,
e: 4,
f: [
5, 6, 7,
{
g: true,
h: false,
i: null,
j: undefined,
l: new Object(),
m: new Array()
}
]
}
}
]
]
]
]
];
function print_r(array, cont){
var b = document.body, s = "<br />", j = 0, type = {}.toString.call(array),
aux = array ? array.length || Object.keys(array).length : 0;
cont = cont || 0;
type = type.split(" ")[1].substr(0, type.split(" ")[1].length - 1);
if (!cont){
b.innerHTML = type + s + tab(cont) + "(" + s;
}
for (var i in array){
type = {}.toString.call(array[i]);
type = type.split(" ")[1].substr(0, type.split(" ")[1].length - 1);
if (/Object|Array/.test(type)){
b.innerHTML += tab(cont + 1) + "[" + i + "] => " + type + s + tab(cont + 1) + "(" + s;
print_r(array[i], cont + 1);
if (j++ == aux - 1){
b.innerHTML += tab(cont) + ")" + s;
}
}
else{
b.innerHTML += tab(cont + 1) + "[" + i + "] => " + array[i] + s;
if (j++ == aux - 1){
b.innerHTML += tab(cont) + ")" + s;
}
}
}
if (!aux){
b.innerHTML += tab(cont) + ")" + s;
}
function tab(n){
for (var j = 0,...