如何在数组javascript中返回一个数组

时间:2017-03-21 18:19:59

标签: javascript angularjs arrays for-loop

我在数组中有一个数组...如何在javascript / angular中使用for循环返回所有值?

例如我的json ......

[
    {
        "Name": "element1",
        "Attributes": [
           {"file":"document.doc"},
           {"file":"document2.doc"}
        ]
    },

    {
        "Name": "element2",
        "Attributes": [
           {"file":"document3.doc"},
           {"file":"document4.doc"}
        ]
    },
    {
        "Name": "element3",
        "Attributes": [
           {"file":"document5.doc"},
           {"file":"document6.doc"}
        ]
    }
]

很难回到属性中的所有文件......似乎每次都会得到第一个。

编辑:

到目前为止我...

function getAllFiles() {
            for (var i = 0; i < Attributes.file.length; i++) {
              return Attributes.file[i];
            }
          }

4 个答案:

答案 0 :(得分:2)

使用Array#reduce

获取所需输出的方法之一

var json = [{Name:"element1",Attributes:[{file:"document.doc"},{file:"document2.doc"}]},{Name:"element2",Attributes:[{file:"document3.doc"},{file:"document4.doc"}]},{Name:"element3",Attributes:[{file:"document5.doc"},{file:"document6.doc"}]}],
    res = json.reduce(function(s,a){
        s.push(...a.Attributes.map(c => c.file));
        return s;
      }, []);
    console.log(res);

<强> ES5

var json = [{Name:"element1",Attributes:[{file:"document.doc"},{file:"document2.doc"}]},{Name:"element2",Attributes:[{file:"document3.doc"},{file:"document4.doc"}]},{Name:"element3",Attributes:[{file:"document5.doc"},{file:"document6.doc"}]}],
    res = json.reduce(function(s,a){
        s = s.concat(a.Attributes.map(c => c.file));
        return s;
      }, []);
    console.log(res);

答案 1 :(得分:1)

try    

var files = [];
json.forEach(function(obj) {
obj.Attributes.forEach(function (f) {
files.push(f.file); })
});

这在json数组上循环然后在每个元素的属性上然后添加file

的vilue

答案 2 :(得分:0)

您可以使用mapreduce等数组方法显示所有文件:

var data = [{
        "Name": "element1",
        "Attributes": [{
                "file": "document.doc"
            },
            {
                "file": "document2.doc"
            }
        ]
    },

    {
        "Name": "element2",
        "Attributes": [{
                "file": "document3.doc"
            },
            {
                "file": "document4.doc"
            }
        ]
    },
    {
        "Name": "element3",
        "Attributes": [{
                "file": "document5.doc"
            },
            {
                "file": "document6.doc"
            }
        ]
    }
];

var files = data.map(function (obj) {
    return obj.Attributes.map(function (i) {
        return i.file;
    });
}).reduce(function (x, y) {
    return x.concat(y);
});

console.log(files);

虽然Kind user的答案更好:

var data = [{
        "Name": "element1",
        "Attributes": [{
                "file": "document.doc"
            },
            {
                "file": "document2.doc"
            }
        ]
    },

    {
        "Name": "element2",
        "Attributes": [{
                "file": "document3.doc"
            },
            {
                "file": "document4.doc"
            }
        ]
    },
    {
        "Name": "element3",
        "Attributes": [{
                "file": "document5.doc"
            },
            {
                "file": "document6.doc"
            }
        ]
    }
];

var files = data.reduce(function(acc, val) {
    return acc.concat(
        val.Attributes.map(function(attribute) {
            return attribute.file;
        })
    );
}, []);

console.log(files);

我重命名了一些变量,以便代码更有意义(对我而言)。

答案 3 :(得分:-1)

像outerArray.innerArray之类的东西。在你的情况下,arrayName.Attributes应该工作。