根据每个对象中的字段过滤对象数组

时间:2019-01-04 10:35:00

标签: angular typescript object ecmascript-6 lodash

假设我有一个这样的对象数组:

[
  { "type": 121, "model": "model1" },
  { "type": 128, "model": "model2" },
  { "type": 130, "model": "model2" },
  { "type": 130, "model": "model2" }
]

我想过滤它并根据 model 字段创建新对象。

最终结果是:

[
  { "type": 121, "model": "model1" }
]

[
  { "type": 128, "model": "model2" },
  { "type": 130, "model": "model2" },
  { "type": 130, "model": "model2" }
]

我正在使用打字稿和lodash,所以最好是这样

我尝试了lodash groupBy和ES6映射,但到目前为止没有成功。我想我可以通过多个forEach循环以肮脏的方式做到这一点,但是我敢肯定,有一种更简单的方法。

6 个答案:

答案 0 :(得分:2)

您可以使用.filter,如下所示:

let newarray1 = array.filter(obj => obj.model === 'model1');
let newarray2 = array.filter(obj => obj.model === 'model2');
...

答案 1 :(得分:1)

您只需使用Array.filter函数即可将数组过滤出新的数组对象。在这里,我使用变量searchModels,该变量将包含要过滤的模型列表,并且在过滤器内部,我正在检查条件searchModels.indexOf(item.model)以检查模型值。

var array = [
  { "type": 121, "model": "model1" },
  { "type": 128, "model": "model2" },
  { "type": 130, "model": "model2" },
  { "type": 130, "model": "model2" }
];


var searchModels = ['model1', 'model3'];
var filteredArray = array.filter((item) => { return searchModels.indexOf(item.model) !== -1 });
console.log("Original: " , array);
console.log("Filtered: " , filteredArray);

答案 2 :(得分:1)

例如,您可以这样:) 在这种情况下,我正在创建地图,我认为它更易于搜索

var newData = new Map([]);

var originalData = [{"type": 130, "model": "model1"}, {"type": 130, "model": "model2"}];
var formattedData = originalData.map(obj => { 
  newData[obj.model].push(obj.type); 
});

您将得到类似的东西:

newData = ([
    [ "model1", "130" ],
    [ "model1", "128" ],
    [ "model2", "3" ]
]);

您可以使用模型键newData [“ model2”] = [“ 3”]来获取任何值

答案 3 :(得分:1)

您可以使用Array.prototype.reduce方法将所有分组。

let array = [
  { "type": 121, "model": "model1" },
  { "type": 128, "model": "model2" },
  { "type": 130, "model": "model2" },
  { "type": 130, "model": "model2" }
]

let all = [...array.reduce((acc, curr) => {
  acc.has(curr.model) ? acc.set(curr.model, [...acc.get(curr.model), curr]): acc.set(curr.model, [curr]);
  return acc;
}, new Map()).values()];
console.log(...all)

答案 4 :(得分:1)

尝试这个@raulicious,

var arrayList= [
  { "type": 121, "model": "model1" },
  { "type": 128, "model": "model2" },
  { "type": 130, "model": "model2" },
  { "type": 131, "model": "model2" }
];


   var filteredArray = [];
   var filtered = [];
   
   arrayList.sort((a, b) => {
         if(a.model == b.model) {
             filtered.push(b);
         } else {
             filtered.push(b);
             filteredArray.push(filtered);
             filtered = [];
           }
       filtered.push(a);
       filteredArray.push(filtered);
     });
    console.log(filteredArray);

我知道这有一些多余的代码,我想尽快减少它

答案 5 :(得分:0)

看看,这对lodash是否有帮助

var data = [
  { "type": 121, "model": "model1" },
  { "type": 128, "model": "model2" },
  { "type": 130, "model": "model2" },
  { "type": 130, "model": "model2" }
];

var grouped = _.groupBy(data, function(item) {
  return item.model;
});

console.log(grouped);
<script src='https://cdn.jsdelivr.net/lodash/4.17.2/lodash.min.js'></script>