遍历JavaScript中的嵌套数组

时间:2019-02-10 18:53:29

标签: javascript arrays

我正在尝试遍历嵌套数组,并且在提取正确的值时遇到了麻烦。

我的Json文件

var regions = [
{
    "id": 265592,
    "longName": "Amsterdam 1",
    "name": "ams01",
    "statusId": 2,
    "regions": [
        {
            "description": "AMS01 - Amsterdam",
            "keyname": "AMSTERDAM",
            "sortOrder": 0
        }
    ]
},
{
    "id": 814994,
    "longName": "Amsterdam 3",
    "name": "ams03",
    "statusId": 2,
    "regions": [
        {
            "description": "AMS03 - Amsterdam",
            "keyname": "AMSTERDAM03",
            "sortOrder": 26
        }
    ]
},
{
    "id": 1004997,
    "longName": "Chennai 1",
    "name": "che01",
    "statusId": 2,
    "regions": [
        {
            "description": "CHE01 - Chennai ",
            "keyname": "CHENNAI",
            "sortOrder": 30
        }
    ]
},

我只想将Regions数组中的Key名称提取到一个数组中。

我的代码工作正常,并给了我输出:

const regions3 = []
for (let i = 0; i < regions.length; i++) {
    const element = regions[i]; 
    const regions1 = (element.regions)
    for (let j = 0; j < regions1.length; j++) {
        const element1 = regions1[j];
        const element2 = element1.keyname;
        regions3.push(element2)
        console.log(regions3)
    }
}

输出

AMSTERDAM
AMSTERDAM03
CHENNAI

我想知道是否有更快的方法来迭代而不是将其运行到两个for循环中?

谢谢

4 个答案:

答案 0 :(得分:5)

您可以将Array.flatMap()Array.map()一起使用spread(在IE / Edge中不受支持):

const regions = [{"id":265592,"longName":"Amsterdam 1","name":"ams01","statusId":2,"regions":[{"description":"AMS01 - Amsterdam","keyname":"AMSTERDAM","sortOrder":0}]},{"id":814994,"longName":"Amsterdam 3","name":"ams03","statusId":2,"regions":[{"description":"AMS03 - Amsterdam","keyname":"AMSTERDAM03","sortOrder":26}]},{"id":1004997,"longName":"Chennai 1","name":"che01","statusId":2,"regions":[{"description":"CHE01 - Chennai ","keyname":"CHENNAI","sortOrder":30}]}]

const result = regions.flatMap(o =>
  o.regions.map(p => p.keyname)
)

console.log(result)

如果您不能使用Array.flatMap(),则可以使用外部Array.map()Array.concat()的结果复制到here is a picture of the dependencies i put in web-inf/lib中:

const regions = [{"id":265592,"longName":"Amsterdam 1","name":"ams01","statusId":2,"regions":[{"description":"AMS01 - Amsterdam","keyname":"AMSTERDAM","sortOrder":0}]},{"id":814994,"longName":"Amsterdam 3","name":"ams03","statusId":2,"regions":[{"description":"AMS03 - Amsterdam","keyname":"AMSTERDAM03","sortOrder":26}]},{"id":1004997,"longName":"Chennai 1","name":"che01","statusId":2,"regions":[{"description":"CHE01 - Chennai ","keyname":"CHENNAI","sortOrder":30}]}]

const result = [].concat(...regions.map(o =>
  o.regions.map(p => p.keyname)
))

console.log(result)

答案 1 :(得分:2)

您可以使用.map()和解构分配

var regions = [{"id":265592,"longName":"Amsterdam 1","name":"ams01","statusId":2,"regions":[{"description":"AMS01 - Amsterdam","keyname":"AMSTERDAM","sortOrder":0}]},{"id":814994,"longName":"Amsterdam 3","name":"ams03","statusId":2,"regions":[{"description":"AMS03 - Amsterdam","keyname":"AMSTERDAM03","sortOrder":26}]},{"id":1004997,"longName":"Chennai 1","name":"che01","statusId":2,"regions":[{"description":"CHE01 - Chennai ","keyname":"CHENNAI","sortOrder":30}]}];

let res = regions.map(({regions: [{keyname}]}) => keyname);

console.log(res);

答案 2 :(得分:2)

另一种解决方案是使用Array::reduce()

var regions = [{"id":265592,"longName":"Amsterdam1","name":"ams01","statusId":2,"regions":[{"description":"AMS01-Amsterdam","keyname":"AMSTERDAM","sortOrder":0}]},{"id":814994,"longName":"Amsterdam3","name":"ams03","statusId":2,"regions":[{"description":"AMS03-Amsterdam","keyname":"AMSTERDAM03","sortOrder":26}]},{"id":1004997,"longName":"Chennai1","name":"che01","statusId":2,"regions":[{"description":"CHE01-Chennai","keyname":"CHENNAI","sortOrder":30}]}];

let res = regions.reduce(
    (acc, curr) => (curr.regions.forEach(x => acc.push(x.keyname)), acc),
    []
);

console.log(res);

答案 3 :(得分:0)

完全一样,但是在JavaScript中使用forEach方法或数组类型可以节省您一些时间。这是一个例子。

const regions3 = [];
        regions.forEach(function(region){
            region.regions.forEach(function(subRegion){
                regions3.push(subRegion.keyname);
                console.log(subRegion.keyname);
            })
        });

forEach执行匿名函数,将数组中的每个元素作为参数传递给匿名函数