从Javascript对象中选择数组

时间:2013-08-14 14:11:09

标签: javascript

拥有以下对象obj

{"Point Ref":[15629989,564646414,65494949],
"Effective Date":["2008-03-03","2010-12-14","2004-10-01"],
"Identifier":["EM","EM","SC"],"Status":["FI","SI","XC"]}"

我可以用

选择第一个数组
obj["Point Ref"]

如何选择更多? (如obj [“Point Ref”,“Identifier”]

3 个答案:

答案 0 :(得分:2)

这将迭代对象中的所有第一级数组:

var obj = {"Point Ref":[15629989,564646414,65494949],
           "Effective Date":["2008-03-03","2010-12-14","2004-10-01"],
           "Identifier":["EM","EM","SC"],"Status":["FI","SI","XC"]};

for (var key in obj) {
    if (obj[key] instanceof Array) {
        console.log(obj[key]);
    }
}

然后,根据您的实际需要,您可以使用多维数组或连接它们。对于多维,您可以这样做:

var multi = [];
....
multi.push(obj[key]);

对于单维数组,您可以这样做:

var single = [];
....
single = single.concat(obj[key]);

答案 1 :(得分:1)

您可以使用concat()从几个现有阵列构建新阵列:

var data = obj["Point Ref"].concat(obj["Identifier"]);

答案 2 :(得分:0)

如果您正在寻找返回两个值的调用,例如在Matlab中说,则不支持。

在此处查看有关返回多个值的备用选项:Return multiple values in JavaScript?