简体   繁体   English

分组或汇总Javascript数组

[英]Group or summarize Javascript array

var json =
[
   {
      id: 11,
      name:"app1",
      family:"apps",
      caseID: 123,
      order:1
   },
   {
      id: 12,
      name:"app1",
      family:"apps",
      caseID: 123,
      order:2
   },
   {
      id: 13,
      name:"app1",
      family:"apps",
      caseID: 123,
      order:3
   },
   {
      id: 14,
      name:"app2",
      family:"tools",
      caseID: 129,
      order:1
   },
   {
      id: 15,
      name:"app2",
      family:"tools",
      caseID: 129,
      order:2
   },
   {
      id: 16,
      name:"app3",
      family:"utils",
      caseID: 120,
      order:1
   },
   {
      id: 17,
      name:"app3",
      family:"utils",
      caseID: 120,
      order:2
   },
      id: 18,
      name:"app3",
      family:"utils",
      caseID: 150,
      order:null
   }
  ] 

Hello, I would like to sort the array above by the highest "order" key and return the filtered array below. 您好,我想按最高的“ order”键对上方的数组进行排序,然后返回下方的过滤后的数组。 The common key is the caseID. 公用密钥是caseID。 Also, If the order key is null return it. 另外,如果订单密钥为null,则将其返回。 I've searched and tested some functions and loops but cannot seem to get it rite. 我已经搜索并测试了一些函数和循环,但似乎无法使其实用。 Any help will be much appreciated. 任何帮助都感激不尽。 I'd prefer es2015 if possible. 如果可能,我希望使用es2015。 Thank you! 谢谢!

filtered = 
 [

  {
      id: 13,
      name:"app1",
      family:"apps",
      caseID: 123,
      order:3  
   },
   {
      id: 15,
      name:"app2",
      family:"tools",
      caseID: 129,
      order:2
   },
   {
      id: 17,
      name:"app3",
      family:"utils",
      caseID: 120,
      order:2
   },
   {
      id: 18,
      name:"app3",
      family:"utils",
      caseID: 150,
      order:null
   }
  ]

I would start by getting rid of dupes. 我将从摆脱骗局开始。 You can do this with reduce() and assigning to an object keyed to caseID . 您可以使用reduce()并将其分配给键为caseID的对象。 You can simultaneously avoid any object with a smaller order than one you've already seen. 您可以同时避免order比已经看到的对象小的任何对象。 Then you can take the values of that hash which will be the unique objects base on caseID and sort them like you normally would. 然后,您可以获取该哈希值(将成为基于caseID的唯一对象)并像往常一样对它们进行排序。 For example: 例如:

 var json = [{ "id": 11, "name":"app1", "family":"apps", "caseID": 123, "order":1},{ "id": 12, "name":"app1", "family":"apps", "caseID": 123, "order":2},{ "id": 13, "name":"app1", "family":"apps", "caseID": 123, "order":3},{ "id": 14, "name":"app2", "family":"tools", "caseID": 129, "order":1},{ "id": 15, "name":"app2", "family":"tools", "caseID": 129, "order":2},{ "id": 16, "name":"app3", "family":"utils", "caseID": 120, "order":1},{ "id": 17, "name":"app3", "family":"utils", "caseID": 120, "order":2},{ "id": 18, "name":"app3", "family":"utils", "caseID": 150, "order":null},] // get just the filtered items based on caseID // picking out only the largest let filtered = json.reduce((a,c) => { if (!a[c.caseID] || a[c.caseID]['order'] < c.order) a[c.caseID] = c return a }, {}) // basic sort let result = Object.values(filtered).sort((a,b) => b.order - a.order) console.log(result) 

You could use a caseID hashtable and override results you find later if order is higher: 如果order较高,则可以使用caseID哈希表并覆盖以后发现的结果:

  const result = [], hash = {};

  for(const el in json) {
   const exists = hash[el.caseId];
   if(exists) {
     if(el.order > exists.order)
       Object.assign(exists, el);
   } else {
       result.push(hash[el.caseId] = {...el});
   }
 }

You can try following 您可以尝试关注

Method 方法

  1. Create an object with unique case ID as key and value being the item with highest order 创建一个对象,该对象具有唯一的案例ID作为键,值是顺序最高的项目
  2. Sort based on order 根据订单排序

 // Code goes here var json = [{"id":11,"name":"app1","family":"apps","caseID":123,"order":1},{"id":12,"name":"app1","family":"apps","caseID":123,"order":2},{"id":13,"name":"app1","family":"apps","caseID":123,"order":3},{"id":14,"name":"app2","family":"tools","caseID":129,"order":1},{"id":15,"name":"app2","family":"tools","caseID":129,"order":2},{"id":16,"name":"app3","family":"utils","caseID":120,"order":1},{"id":17,"name":"app3","family":"utils","caseID":120,"order":2},{"id":18,"name":"app3","family":"utils","caseID":150,"order":null}]; var map = {}; // Create a map of unique case ID's with highest order json.forEach((item) => { if(map[item.caseID]) { if(map[item.caseID].order < item.order) { map[item.caseID] = item; } } else { map[item.caseID] = item; } }); // Sorting the array based on order var result = Object.values(map).sort((a,b) => b.order-a.order); console.log(result); 

In ES6: 在ES6中:

json.sort((a, b) => a.caseID > b.caseID);
let bad_order = json.filter(v => v.order === null);
let good_order = json.filter(v => v.order !== null);

Example

In ES5: 在ES5中:

json.sort(function(a, b) { return a.caseID > b.caseID; });

var bad_order = [];
var good_order = [];

for(var i = 0; i < json.length; i++){
  if(json[i].order === null)
    bad_order.push(json[i]);
  else
    good_order.push(json[i]);
}

Example

Use reduce method to create an object where the keys will be the caseID .While creating the object check if the value of the order is more or less that the current order value.If the current value is less the than the new value, replace it with new value. 使用reduce方法创建一个键为caseID的对象。创建对象时,检查订单值是否大于或小于当前订单值;如果当前值小于新订单值,则将其替换具有新的价值。

Then use Object.values(object) to create an array of values from the object 然后使用Object.values(object)从对象创建一个值数组

 var json = [{ "id": 11, "name": "app1", "family": "apps", "caseID": 123, "order": 1 }, { "id": 12, "name": "app1", "family": "apps", "caseID": 123, "order": 2 }, { "id": 13, "name": "app1", "family": "apps", "caseID": 123, "order": 3 }, { "id": 14, "name": "app2", "family": "tools", "caseID": 129, "order": 1 }, { "id": 15, "name": "app2", "family": "tools", "caseID": 129, "order": 2 }, { "id": 16, "name": "app3", "family": "utils", "caseID": 120, "order": 1 }, { "id": 17, "name": "app3", "family": "utils", "caseID": 120, "order": 2 }, { "id": 18, "name": "app3", "family": "utils", "caseID": 150, "order": null } ] var m = json.reduce(function(acc, curr, index) { if (acc[curr['caseID']] === undefined) { acc[curr['caseID']] = curr; } else { if (acc[curr['caseID']].order < curr.order) { acc[curr['caseID']] = curr; } } return acc; }, {}) console.log(Object.values(m)) 

This should help you filter array of objects. 这应该可以帮助您过滤对象数组。

            var filteredMap = {};
            json.forEach(function (item) {
                filteredMap[item.caseID] = item;
            });
            var filteredArray = [];
            for (var key in filteredMap) {
                filteredArray.push(filteredMap[key]);
            }
            console.log(JSON.stringify(filteredArray));

Sort by order and caseID and then filter by caseID,Here is the code: 按订单和caseID排序,然后按caseID过滤,这是代码:

var json =
[
   {
      id: 11,
      name:"app1",
      family:"apps",
      caseID: 123,
      order:1
   },
   {
      id: 12,
      name:"app1",
      family:"apps",
      caseID: 123,
      order:2
   },
   {
      id: 13,
      name:"app1",
      family:"apps",
      caseID: 123,
      order:3
   },
   {
      id: 14,
      name:"app2",
      family:"tools",
      caseID: 129,
      order:1
   },
   {
      id: 15,
      name:"app2",
      family:"tools",
      caseID: 129,
      order:2
   },
   {
      id: 16,
      name:"app3",
      family:"utils",
      caseID: 120,
      order:1
   },
   {
      id: 17,
      name:"app3",
      family:"utils",
      caseID: 120,
      order:2
   }, {
      id: 18,
      name:"app3",
      family:"utils",
      caseID: 150,
      order:null
   }
 ]
var obj = {}
var arr = json.sort(function(a, b) {
  return b.order - a.order
}).sort(function(a, b) {
  return a.caseId - b.caseId
}).filter(function(item, index, array){
  return obj.hasOwnProperty(item.caseID) ? false : (obj[item.caseID] = true)
})
console.log(arr)

demo: http://jsbin.com/qabehorike/edit?js,console,output 演示: http : //jsbin.com/qabehorike/edit?js,控制台,输出

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

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