从一个数组中查找属性的最大值并将其传递给新数组

时间:2015-12-02 14:03:54

标签: javascript jquery arrays

我有一个对象如下

[{ "ResourceId": "11a0a500-7e1c-4e2d-810bbd4401892b7c", "BucketValue": 8, "WeekId": 1 },
{ "ResourceId": "11a0a500-7e1c-4e2d-810b-bd4401892b7c", "BucketValue": 8, "WeekId": 0 },
{ "ResourceId": "11a0a500-7e1c-4e2d-810b-bd4401892b7c", "BucketValue": 16, "WeekId": 0 },
{ "ResourceId": "11a0a500-7e1c-4e2d-810b-bd4401892b7c", "BucketValue": 24, "WeekId": 0 },
{ "ResourceId": "11a0a500-7e1c-4e2d-810b-bd4401892b7c", "BucketValue": 32, "WeekId": 0 },
{ "ResourceId": "11a0a500-7e1c-4e2d-810b-bd4401892b7c", "BucketValue": 16, "WeekId": 1 },
{ "ResourceId": "11a0a500-7e1c-4e2d-810b-bd4401892b7c", "BucketValue": 24, "WeekId": 1 },
{ "ResourceId": "11a0a500-7e1c-4e2d-810b-bd4401892b7c", "BucketValue": 40, "WeekId": 0 },
{ "ResourceId": "11a0a500-7e1c-4e2d-810b-bd4401892b7c", "BucketValue": 32, "WeekId": 1 }]

我需要做的是在BucketValueResourceId匹配

的情况下找到WeekId的最大值

即。在上面的示例中,如果BucketValueResourceId = 11a0a500-7e1c-4e2d-810b-bd4401892b7cWeekId = 0,则查找40的最大值。

如果ResourceId = 11a0a500-7e1c-4e2d-810b-bd4401892b7cWeekId = 1,则最多为32

我应该有一个最终数组如下:

[{
    "ResourceId": "11a0a500-7e1c-4e2d-810b-bd4401892b7c",
    "BucketValue": 40,
    "WeekId": 0
},{
    "ResourceId": "11a0a500-7e1c-4e2d-810b-bd4401892b7c",
    "BucketValue": 32,
    "WeekId": 1
}]

2 个答案:

答案 0 :(得分:1)

我有两个可能解决此问题的方法:

将数组排序为Array.prototype.sort(),以WeekId为单位进行排序,并仅使用Array.prototype.filter()的每个组的第一项。



var array = [{ "ResourceId": "11a0a500-7e1c-4e2d-810bb-d4401892b7c", "BucketValue": 8, "WeekId": 1 }, { "ResourceId": "11a0a500-7e1c-4e2d-810b-bd4401892b7c", "BucketValue": 8, "WeekId": 0 }, { "ResourceId": "11a0a500-7e1c-4e2d-810b-bd4401892b7c", "BucketValue": 16, "WeekId": 0 }, { "ResourceId": "11a0a500-7e1c-4e2d-810b-bd4401892b7c", "BucketValue": 24, "WeekId": 0 }, { "ResourceId": "11a0a500-7e1c-4e2d-810b-bd4401892b7c", "BucketValue": 32, "WeekId": 0 }, { "ResourceId": "11a0a500-7e1c-4e2d-810b-bd4401892b7c", "BucketValue": 16, "WeekId": 1 }, { "ResourceId": "11a0a500-7e1c-4e2d-810b-bd4401892b7c", "BucketValue": 24, "WeekId": 1 }, { "ResourceId": "11a0a500-7e1c-4e2d-810b-bd4401892b7c", "BucketValue": 40, "WeekId": 0 }, { "ResourceId": "11a0a500-7e1c-4e2d-810b-bd4401892b7c", "BucketValue": 32, "WeekId": 1 }],
    weekId,        
    result = array.sort(function (a, b) {
        return a.WeekId - b.WeekId || b.BucketValue - a.BucketValue;
    }).filter(function (a) {
        if (weekId !== a.WeekId) {
            weekId = a.WeekId;
            return true;
        }
    });

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
&#13;
&#13;
&#13;

或者使用Array.prototype.reduce()并在结果集中将每个较大的值替换为相同的WeekId

&#13;
&#13;
var array = [{ "ResourceId": "11a0a500-7e1c-4e2d-810bb-d4401892b7c", "BucketValue": 8, "WeekId": 1 }, { "ResourceId": "11a0a500-7e1c-4e2d-810b-bd4401892b7c", "BucketValue": 8, "WeekId": 0 }, { "ResourceId": "11a0a500-7e1c-4e2d-810b-bd4401892b7c", "BucketValue": 16, "WeekId": 0 }, { "ResourceId": "11a0a500-7e1c-4e2d-810b-bd4401892b7c", "BucketValue": 24, "WeekId": 0 }, { "ResourceId": "11a0a500-7e1c-4e2d-810b-bd4401892b7c", "BucketValue": 32, "WeekId": 0 }, { "ResourceId": "11a0a500-7e1c-4e2d-810b-bd4401892b7c", "BucketValue": 16, "WeekId": 1 }, { "ResourceId": "11a0a500-7e1c-4e2d-810b-bd4401892b7c", "BucketValue": 24, "WeekId": 1 }, { "ResourceId": "11a0a500-7e1c-4e2d-810b-bd4401892b7c", "BucketValue": 40, "WeekId": 0 }, { "ResourceId": "11a0a500-7e1c-4e2d-810b-bd4401892b7c", "BucketValue": 32, "WeekId": 1 }],
    result = array.reduce(function (r, a) {
        !r.some(function (b, i, bb) {
            if (a.WeekId === b.WeekId) {
                if (a.BucketValue > b.BucketValue) {
                    bb[i] = a;
                }
                return true;
            }
        }) && r.push(a);
        return r;
    }, []);

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
&#13;
&#13;
&#13;

答案 1 :(得分:0)

在我看来,你最好有一个函数返回一个搜索的结果,然后将该结果添加到一个新数组,而不是让它返回所有搜索的数组。

function findMax(arr, id, week) {

  // if week is not present in any of the objects
  // immediately return false from the function
  if (!arr.some(function (el) { return el.WeekId === week; })) return false;

  // find the max ResourceId value from the data set
  var max = Math.max.apply(null, arr.filter(function (el) {
    return el.ResourceId === id && el.WeekId === week;
  }).map(function (el) { return el.BucketValue; }));

  // return the object that has that max ResourceId value
  return arr.filter(function (el) {
    return el.BucketValue === max;
  })[0];

}

var arr = [];
arr.push(findMax(data, '11a0a500-7e1c-4e2d-810b-bd4401892b7c', 0));
arr.push(findMax(data, '11a0a500-7e1c-4e2d-810b-bd4401892b7c', 1));

DEMO

或者,您可以让函数返回所有搜索的数组。

function findMax(arr, search) {

  var out = [];

  search.forEach(function (item) {

    var max = Math.max.apply(null, arr.filter(function (el) {
      return el.ResourceId === item[0] && el.WeekId === item[1];
    }).map(function (el) {
      return el.BucketValue;
    }));

    var obj = arr.filter(function (el) {
      return el.BucketValue === max;
    })[0];

    out.push(obj);

  });

  return out;

}

var search = [
  ['11a0a500-7e1c-4e2d-810b-bd4401892b7c', 0],
  ['11a0a500-7e1c-4e2d-810b-bd4401892b7c', 1]
];

findMax(data, search);

DEMO

修改

如果您只想从对象返回最大值,而不是从整个对象返回过滤器中的BucketValue

function findMax(arr, id, week) {

  // find the max ResourceId value from the data set
  var max = Math.max.apply(null, arr.filter(function (el) {
    return el.ResourceId === id && el.WeekId === week;
  }).map(function (el) { return el.BucketValue; }));

  // return the object that has that max ResourceId value
  return arr.filter(function (el) {
    return el.BucketValue === max;
  })[0].BucketValue;

}