如何根据字段的值过滤对象数组并重新映射对象?

时间:2014-06-05 06:11:57

标签: javascript lodash

我有以下对象:

options.getOrderBy = function (type) {
    var OrderBy = [
        { type: 'x', id: 0, label: 'xx1', key: 'yy1' },
        { type: 'x', id: 1, label: 'xx2', key: [1,2] },
        { type: 'x', id: 9, label: 'xx2', key: ['a','b'] },
        { type: 'y', id: 0, label: 'xx44', key: 'yya' },
        { type: 'y', id: 1, label: 'xx45', key: 'yyb' },
        { type: 'y', id: 2, label: 'xx46', key: 'yyc' },
    ];
    return OrderBy;
};

我需要的是,当使用(' x')类型调用函数时,我希望它返回类似的内容:

[
   { id: 0, label: 'xx1', key: [1.2] },
   { id: 1, label: 'xx2', key: 'yy2' },
   { id: 9, label: 'xx2', key: ['a','b'] }
]

有人可以向我解释如何根据类型字段的值过滤数组,然后返回包含id,label和key的对象数组?

请注意,我有_lodash,如果它更容易,我想使用它。我的解决方案也适用于大于IE9的浏览器

4 个答案:

答案 0 :(得分:1)

options.getOrderBy = function (type) {
    var OrderBy = [
        { type: 'x', id: 0, label: 'xx1', key: 'yy1' },
        { type: 'x', id: 1, label: 'xx2', key: 'yy2' },
        { type: 'y', id: 0, label: 'xx44', key: 'yya' },
        { type: 'y', id: 1, label: 'xx45', key: 'yyb' },
        { type: 'y', id: 2, label: 'xx46', key: 'yyc' },
    ];
    return OrderBy.filter(function(e) {
        return e.type === type;
    }).map(function(e) {
        delete e.type;
        return e;
    });
};

如果您可以将OrderBy结构更改为对象,则会更简单:

options.getOrderBy = function (type) {
    var OrderBy = {
      x:[ 
          { id: 0, label: 'xx1', key: 'yy1' },
          {  id: 1, label: 'xx2', key: 'yy2' }
        ],
      y:[
          { id: 0, label: 'xx44', key: 'yya' },
          { id: 1, label: 'xx45', key: 'yyb' },
          { id: 2, label: 'xx46', key: 'yyc' }
        ]
    };
    return OrderBy[type];
};

答案 1 :(得分:0)

您可以按以下方式过滤订单选项:

var newOrderBy = [];
for(var i = 0, l = OrderBy.length; i < l; i++) {
  if(OrderBy[i].type == type) {
    newOrderBy.push({
      id: OrderBy[i].id,
      label: OrderBy[i].label,
      key: OrderBy[i].key
    });
  }
}

答案 2 :(得分:0)

使用underscore.js _.filter(): -

options.getOrderBy = function (type) {
var OrderBy = [
    { type: 'x', id: 0, label: 'xx1', key: 'yy1' },
    { type: 'x', id: 1, label: 'xx2', key: [1,2] },
    { type: 'x', id: 9, label: 'xx2', key: ['a','b'] },
    { type: 'y', id: 0, label: 'xx44', key: 'yya' },
    { type: 'y', id: 1, label: 'xx45', key: 'yyb' },
    { type: 'y', id: 2, label: 'xx46', key: 'yyc' },
];
var filteredOrderBy = _.filter(OrderBy, function (order) {
    if (order.type === type) {
        return true;
    } else return false;
});
return filteredOrderBy ;

};

答案 3 :(得分:0)

我的解决方案利用了Lodash的mapomit功能。见here

var filter = function(collection, filterKey) {
    return _.map(collection, function(elem) {
        return _.omit(elem, filterKey);
    })        
}

将其用作:

var filtered = filter(OrderBy, 'type');

编辑:还考虑了filterKey

的值
var filter = function(collection, filterKey, filterValue) {
                 return _.compact(_.map(collection, function(elem) {
                     if(elem[filterKey] === filterValue)
                         return _.omit(elem, filterKey);
                 }))        
              }

将其用作:

var filtered = filter(OrderBy, 'type', 'x');

第二次编辑:更清晰的版本

var filterV2 = function(collection, filter) {
    return _(collection)
           .map(function (elem) { if(elem[filter.key] == filter.value) return _.omit(elem, filter.key) })
           .compact()
           .value()
} 

将其用作:

var filtered = filterV2(OrderBy, { key: 'type', value: 'x' });