2017-01-20 3 views
0

現在オブジェクトをループしようとしていますが、そのオブジェクトにはオブジェクトと配列が混在しています。オブジェクト内の動的キーをループすると配列内に出力されないようです

オブジェクトオブジェクトをループ

var templateObject = { 
    "addressbook": { 
     "streetaddress": ["streetaddress1", "1"], 
     "country": ["country", "2"] 
    }, 
    "companyname": ["thecompanyname", "1"], 
    "email": ["theemail", "1"] 
}; 

、それはだ、私たちは常にオブジェクトにあるキーの名前を知ることができませんので、私は

for(var prop in templateObject) 
{ 
    document.write(prop); 
    if(templateObject.hasOwnProperty(prop)) 
    { 
     for(var subItem in templateObject[prop]) 
     { 
      var currentItem = templateObject[prop][subItem]; 
      document.write('<b>' + currentItem[0] + '</b><br/>'); 
      document.write(currentItem[1]);   
      document.write('<hr/>'); 
     } 
    } 
} 

ループにそれらをしようとしています現在返されている: -

アドレス帳アドレス1国2会社名th 1未定義 メールth 1未定義

私は問題なくアドレス帳オブジェクトを取得できるようですが、会社名やメールアドレスを取得して画面に表示することはできません。

私は私がここにもフィドル用意しました

しばらくの間、このいずれかで苦労してきたとして、誰かがこれで私を助けることができる願っています:事前にhttps://jsfiddle.net/dimmers/97mqke0f/

感謝を

+0

あなたは、あなたがオブジェクトのプロパティを見つけた場合、再帰関数を呼び出し、関数にループをひそかにする必要があります。 – RobG

答えて

0

お使いのオブジェクトにObject.keys関数を試してください、それは非常にいいです。

Object.keys(templateObject)を行うと、あなたにaddressbookを与える必要があり、companynameemail

またvar inはオブジェクトのみのためです。配列なので、会社名や電子メールでは機能しません。

は、これが私の作品:

var templateObject = { 
    "addressbook": { 
     "streetaddress": ["streetaddress1", "1"], 
     "country": ["country", "2"] 
    }, 
    "companyname": ["thecompanyname", "1"], 
    "email": ["theemail", "1"] 
}; 


function writeIt(currentItem) { 
    // currentItem should be array 
    document.write('<b>' + currentItem[0] + '</b><br/>'); 
    document.write(currentItem[1]); 
    document.write('<hr/>'); 
} 

for (var prop in templateObject) { 
    document.write(prop); 
    if (Object.prototype.toString.call(templateObject[prop]) == '[object Object]') { 
     for (var subItem in templateObject[prop]) { 
      var currentItem = templateObject[prop][subItem]; 
      writeIt(currentItem); 

     } 
    } else if (Object.prototype.toString.call(templateObject[prop]) == '[object Array]') { 
     writeIt(templateObject[prop]); 
    } 
} 

更新フィドル: https://jsfiddle.net/97mqke0f/1/

+1

"* for..inはcompanynameまたはemailで配列*であるため動作しません。 oops、* for..in *は配列に対して機能します(疎配列の場合は便利です)。しかし、索引以上のものを返すことがあり、順序が合わない可能性があるため推奨しません。 – RobG

+0

ああ、あの情報のおかげで@RobG私は答えを提供することによって何かを学んだ、私はStackOverflowが大好き! :) – Noitidart

0

あなたはその後、オブジェクトのプロパティのために再帰的に呼び出し、キーを取得するための関数を作成することができます(すなわちtypeof value == 'object')。しかし、配列である値を反復処理したくない場合は、Array.isArrayを最初に使用してチェックしてください。

function showKeys(obj) { 
 

 
    // Get the keys of object and show them 
 
    var keys = Object.keys(obj); 
 
    document.write('<br>' + keys.join(', ')); 
 

 
    // Check for values that are Objects, ignore Arrays, and call recursively 
 
    keys.forEach(function(key) { 
 
    var value = obj[key]; 
 
    if (typeof value == 'object' && !Array.isArray(value)) { 
 
     showKeys(value); 
 
    } 
 
    }); 
 
} 
 

 
var templateObject = { 
 
    "addressbook": { 
 
     "streetaddress": ["streetaddress1", "1"], 
 
     "country": ["country", "2"] 
 
    }, 
 
    "companyname": ["thecompanyname", "1"], 
 
    "email": ["theemail", "1"] 
 
}; 
 

 
showKeys(templateObject);

これは、ネストの任意の深さにも対応しますが、配列内のオブジェクトを扱うことはありませんが、それは同様に配列を超えるものをあまりにも(ループを取得するには些細なのですが、印刷しませんそれらのキー)。

0

これまで同様の問題が発生しましたが、解決するための関数を書いています。ここで私のコードは、オブジェクトをループし、すべての列挙可能なプロパティを出力します。

function joOutput(o, decodeUrl) { 
 
    var txt, depth, sp, sub, isEmpty; 
 
    if (typeof o !== "object") 
 
    return "[Not an object]"; 
 
    isEmpty = function(e) { 
 
    var i; 
 
    for (i in e) 
 
     return false; 
 
    return true; 
 
    }; 
 
    if (isEmpty(o)) 
 
    return "[Empty Object]"; 
 
    txt = "<b>NOTE:</b>n for attribute name, d for depth, v for value.<br>"; 
 
    txt += "-----------------------------------<br>"; 
 
    depth = 0; 
 

 
    sp = function(n) { 
 
    var s = ""; 
 
    for (var i = 0; i < n; i++) { 
 
     s += "&nbsp&nbsp&nbsp&nbsp&nbsp."; 
 
    } 
 
    return s; 
 
    }; 
 
    sub = function(obj) { 
 
    var attr; 
 
    for (attr in obj) { 
 
     if ((typeof obj[attr]) !== "object") { 
 
     if (decodeUrl) 
 
      obj[attr] = decodeURIComponent(obj[attr]); 
 
     txt += sp(depth) + "[n: " + attr + " - d: " + depth + " - v: <b>" + obj[attr] + "</b>]<br>"; 
 
     } else { 
 
     txt += sp(depth) + "[n:" + attr + " - d:" + depth + "]...<br>"; 
 
     depth++; 
 
     arguments.callee(obj[attr]); 
 
     } 
 
    } 
 
    depth--; 
 
    return txt; 
 
    }; 
 
    return sub(o); 
 
} 
 

 
// Test: 
 
var templateObject = { 
 
    "addressbook": { 
 
     "streetaddress": ["streetaddress1", "1"], 
 
     "country": ["country", "2"] 
 
    }, 
 
    "companyname": ["thecompanyname", "1"], 
 
    "email": ["theemail", "1"] 
 
}; 
 

 
var txt = joOutput(templateObject); 
 
document.write(txt);

関連する問題