【问题标题】:how to return an array inside an array javascript如何在数组javascript中返回一个数组
【发布时间】:2017-03-21 18:19:58
【问题描述】:

我在数组中有一个数组...如何在 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];
            }
          }

【问题讨论】:

  • 请发布 1) 预期的输出应该是什么,以及 2) 你到目前为止尝试了什么。
  • "...似乎每次都只得到第一个。"向我们展示发生这种情况的代码,我们可以指出它有什么问题。跨度>

标签: javascript angularjs arrays for-loop


【解决方案1】:

获得所需输出的方法之一,使用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);

【讨论】:

  • 这行得通...但我正在使用 underscore.js 使其与 angularjs 一起工作...所以当我开始工作时会更新
  • @jeremy 怎么样?有效吗?
【解决方案2】:
try    

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

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

【讨论】:

  • hmm 这只是返回一个空数组,但得到正确数量的空数组
  • 这是基于上面的确切 json 结构,尽管我想知道getAllFiles() 如何访问Attributes.file ...而Attributes 是上述结构中的一个数组..
【解决方案3】:

您可以使用 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);

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

【讨论】:

    【解决方案4】:

    类似于 outerArray.innerArray。在您的情况下,arrayName.Attributes 应该可以工作。

    【讨论】:

      猜你喜欢
      • 2015-09-24
      • 2021-04-10
      • 2021-11-13
      • 2017-05-02
      • 2020-09-03
      • 1970-01-01
      • 2023-03-06
      • 2013-04-04
      • 2014-11-22
      相关资源
      最近更新 更多