それはパターンが同じでない場合であっても動作しますので、この1つは再帰を使用しています:
function looopAllTheThings(theThings, theCallback, depth){
if(undefined===depth) depth = 0;
if(typeof theThings == "object"){
for(var p in theThings)
if(theThings.hasOwnProperty(p))
if("object" == typeof theThings[p] ||
"array" == typeof theThings[p])
looopAllTheThings(theThings[p], theCallback, (depth+1));
else theCallback(p, theThings[p], depth);
}else if(typeof theThings == "array"){
for(var i=0; i<theThings.length; i++)
if("object" == typeof theThings[i] ||
"array" == typeof theThings[i])
looopAllTheThings(theThings[i], theCallback, (depth+1));
else theCallback(p, theThings[i], depth);
}else{
theCallback(null, theThings, depth);
}
}
それを使用しますこのように:
looopAllTheThings(data, function(key, value, depth){
document.getElementById('out').innerHTML += ("-".repeat(depth))+" "+key+" = "+value+"<br>";
});
はここでフィドルです:https://jsfiddle.net/2o2Lyayj/
あなたは1 'for'ループを持っている、そして、そこの内側に、あなたは別の' for'ループを持っています。コードを表示すると、より具体的な回答が得られるはずです。 –