2014-04-11 103 views
0

我在下面有下面的代碼。我想訪問obj數組內的數組(名爲link1和link2)。對象數組中的返回數組元素

$(function() { 
    var storage = chrome.storage.local; 
    storage.clear(); 
    var link1 = 'myLinks1'; 
    var link2 = 'myLinks2'; 

    var obj= {}; 

    obj[link1] = ['www.google.com', 'yahoo.com', 'www.msn.com']; 
    obj[link2] = ['www.microsoft.com', 'live.com', 'www.espn.com']; 

    storage.set(obj); 

    storage.get(null,function(result){ 
     //returns obj with array of links inside 
     console.log(result); 
     // Returns array of obj but doesn't contain array of links inside. 
     console.log(Object.keys(result)); 
    }); 
}); 

編輯:我希望能夠訪問該鏈接,如www.Google.com

感謝

+0

什麼_does_最後爲你輸出線路嗎?基於代碼,我希望它返回'[「myLinks1」,「myLinks2」]' –

+0

'console.log(Object.keys(result).reduce(function(arr,k){return arr.concat(result [k])},[]));' – raina77ow

回答

0

你應該能夠獲得通過書面結果鍵和數組值for循環以下

Object.keys(obj).forEach(function(key) { 
    console.log(key, obj[key]); 
    for (var i in obj[key]) 
    console.log(obj[key][i]); 
}); 

輸出:

myLinks1 [ 「www.google.com」, 「yahoo.com」, 「www.msn.com」]
www.google.com
yahoo.com
www.msn.com
myLinks2 [」 www.microsoft.com」, 「live.com」, 「www.espn.com」]
www.microsoft.com
live.com
www.espn.com