简体   繁体   English

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

[英]Extract object attribute from list of objects in Javascript

I have the following object that I'm receiving from an API: 我从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' },
    ]
}

Then I want to have another variable named types of type Array that will hold every possible value of the type attribute of each one of the objects. 然后我想要另一个名为Array types的变量,它将保存每个对象的type属性的每个可能值。 In this case it would be: 在这种情况下,它将是:

types = ['A', 'B', 'C']

I'm trying to have it done in a functional way (I'm using underscore.js) but I'm unable to figure out a way of doing it. 我试图以功能方式完成它(我使用的是underscore.js),但我无法找到一种方法。 Right now I'm using 现在我正在使用

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

But that's very ugly. 但那非常难看。 Can you help me in figuring out a better way of writing this code? 你能帮我找出更好的编写代码的方法吗?

Thanks! 谢谢!

This should work: 这应该工作:

types = _.chain(input) // enable chaining
  .values()            // object to array
  .flatten()           // 2D array to 1D array
  .pluck("type")       // pick one property from each element
  .uniq()              // only the unique values
  .value()             // get an unwrapped array

Fiddle: http://jsfiddle.net/NFSfs/ 小提琴: http//jsfiddle.net/NFSfs/

Of course, you can remove all whitespace if you want to: 当然,如果您愿意,可以删除所有空格:

types = _.chain(input).values().flatten().pluck("type").uniq().value()

or without chaining: 或没有链接:

types = _.uniq(_.pluck(_.flatten(_.values(input)),"type"));

flatten seems to work on objects , even though the documentation clearly states it shouldn't . flatten似乎在对象上工作 ,即使文档明确指出它不应该 If you wish to code against implementation, you can leave out the call to values , but I don't recommend that. 如果您希望对实现进行编码,则可以省略对values的调用,但我不建议这样做。 The implementation could change one day, leaving your code mysteriously broken. 实施可能会在一天内发生变化,使您的代码神秘地破裂。

If you just want shorter code, you could flatten the objects into a single Array, then map that Array. 如果您只想要更短的代码,可以将对象展平为单个数组,然后映射该数组。

var types = _.unique(_.map(_.flatten(_.toArray(response)), function(arr) {
    return arr.type;
}));

Here's another version. 这是另一个版本。 Mostly just for curiosity's sake. 主要是出于好奇心的缘故。

var types = _.unique(_.pluck(_.reduce(response, _.bind(Function.apply, [].concat), []), "type"));

Here's another one. 这是另一个。

var types = _.unique(_.reduce(response, function(acc, arr) {
    return acc.concat(_.pluck(arr,"type"));
}, []));

And another. 而另一个。

var types = _.unique(_.pluck([].concat.apply([], _.toArray(response)), "type"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM