返回Object数组内的Array Element

时间:2014-04-11 14:39:57

标签: javascript arrays object

我在下面有这个代码。我想访问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等链接

由于

1 个答案:

答案 0 :(得分: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