相关疑难解决方法(0)

从Javascript中的对象列表中提取对象属性

我从API接收到以下对象:

{
   '2012-12-12': [
       { 'id': 1234,
         'type': 'A' },
       { 'id': 1235,
         'type': 'A' },
       { 'id': 1236,
         'type': 'B' },
    ],
   '2012-12-13': [
       { 'id': 1237,
         'type': 'A' },
       { 'id': 1238,
         'type': 'C' },
       { 'id': 1239,
         'type': 'B' },
    ]
}
Run Code Online (Sandbox Code Playgroud)

然后我想要另一个名为typestype的变量Array,它将保存每个type对象的属性的每个可能值.在这种情况下,它将是:

types = ['A', 'B', 'C']
Run Code Online (Sandbox Code Playgroud)

我试图以功能方式完成它(我使用的是underscore.js),但我无法找到一种方法.现在我正在使用

types = [];
_.each(response, function(arr1, key1) {
    _.each(arr1, function(arr2, key2) {
        types.push(arr2.type);
    });
});
types = _.uniq(types);
Run Code Online (Sandbox Code Playgroud)

但那非常难看.你能帮我找出更好的编写代码的方法吗?

谢谢!

javascript functional-programming underscore.js

3
推荐指数
1
解决办法
1366
查看次数