获取对象数组中唯一值的最高值

时间:2018-01-12 09:25:33

标签: jquery

我有以下数组:

var arr = [{
  "id": 1,
  "versionID": 1,
  "versionNR": 1,
  "format": "<div><label for=\"Part_1\">Part 1</label><input data-id=\"1\" id=\"Part_1\" type=\"checkbox\" /></div>"
}, {
  "id": 65,
  "versionID": 65,
  "versionNR": 1,
  "format": "<div><label for=\"Part_65\">Part 2</label><input data-id=\"65\" id=\"Part_65\" type=\"checkbox\" /></div>"
}, {
  "id": 92,
  "versionID": 1,
  "versionNR": 2,
  "format": "<div><label for=\"Part_92\">Part 1</label><input data-id=\"92\" id=\"Part_92\" type=\"checkbox\" /></div>"
}, {
  "id": 93,
  "versionID": 65,
  "versionNR": 2,
  "format": "<div><label for=\"Part_93\">Part 2</label><input data-id=\"93\" id=\"Part_93\" type=\"checkbox\" /></div>"
}, {
  "id": 96,
  "versionID": 65,
  "versionNR": 3,
  "format": "<div><label for=\"Part_96\">Part 2</label><input data-id=\"96\" id=\"Part_96\" type=\"checkbox\" /></div>"
}]

var dupes = {};
var singles = [];

$.each(arr, function(i, el) {

    if (!dupes[el.versionID]) {
        dupes[el.versionID] = true;
        singles.push(el);
    }
});

console.log(singles)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

我想要实现的是每个唯一versionID并返回最高versionNR

的对象

我想要的结果是:

var arr = [{
  "id": 92,
  "versionID": 1,
  "versionNR": 2,
  "format": "<div><label for=\"Part_92\">Part 1</label><input data-id=\"92\" id=\"Part_92\" type=\"checkbox\" /></div>"
},{
  "id": 96,
  "versionID": 65,
  "versionNR": 3,
  "format": "<div><label for=\"Part_96\">Part 2</label><input data-id=\"96\" id=\"Part_96\" type=\"checkbox\" /></div>"
}]
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

正如您可以看到第一个代码示例,它返回正确的versionID = 1 & 65,但versionNR1而不是最高。

我很想得到我想要的结果。有人可以帮助我。

3 个答案:

答案 0 :(得分:1)

您可以使用array#reduce根据versionID对数组进行分组。如果对象已包含versionID,则比较versionNR并存储具有最高值的对象。

var arr = [{
  "id": 1,
  "versionID": 1,
  "versionNR": 1,
  "format": "<div><label for=\"Part_1\">Part 1</label><input data-id=\"1\" id=\"Part_1\" type=\"checkbox\" /></div>"
}, {
  "id": 65,
  "versionID": 65,
  "versionNR": 1,
  "format": "<div><label for=\"Part_65\">Part 2</label><input data-id=\"65\" id=\"Part_65\" type=\"checkbox\" /></div>"
}, {
  "id": 92,
  "versionID": 1,
  "versionNR": 2,
  "format": "<div><label for=\"Part_92\">Part 1</label><input data-id=\"92\" id=\"Part_92\" type=\"checkbox\" /></div>"
}, {
  "id": 93,
  "versionID": 65,
  "versionNR": 2,
  "format": "<div><label for=\"Part_93\">Part 2</label><input data-id=\"93\" id=\"Part_93\" type=\"checkbox\" /></div>"
}, {
  "id": 96,
  "versionID": 65,
  "versionNR": 3,
  "format": "<div><label for=\"Part_96\">Part 2</label><input data-id=\"96\" id=\"Part_96\" type=\"checkbox\" /></div>"
}];
var result = Object.values(arr.reduce((r,o) => {
  if(o.versionID in r) {
    if(o.versionNR > r[o.versionID].versionNR)
      r[o.versionID] = Object.assign({},o);
  } else {
    r[o.versionID] = Object.assign({}, o);
  }
  return r;
},{}));

console.log(result)

答案 1 :(得分:1)

我添加了一个没有reduce的答案,因为包括我自己在内的一些人仍然不放心。

var arr = [{
  "id": 1,
  "versionID": 1,
  "versionNR": 1,
  "format": "<div><label for=\"Part_1\">Part 1</label><input data-id=\"1\" id=\"Part_1\" type=\"checkbox\" /></div>"
}, {
  "id": 65,
  "versionID": 65,
  "versionNR": 1,
  "format": "<div><label for=\"Part_65\">Part 2</label><input data-id=\"65\" id=\"Part_65\" type=\"checkbox\" /></div>"
}, {
  "id": 92,
  "versionID": 1,
  "versionNR": 2,
  "format": "<div><label for=\"Part_92\">Part 1</label><input data-id=\"92\" id=\"Part_92\" type=\"checkbox\" /></div>"
}, {
  "id": 93,
  "versionID": 65,
  "versionNR": 2,
  "format": "<div><label for=\"Part_93\">Part 2</label><input data-id=\"93\" id=\"Part_93\" type=\"checkbox\" /></div>"
}, {
  "id": 96,
  "versionID": 65,
  "versionNR": 3,
  "format": "<div><label for=\"Part_96\">Part 2</label><input data-id=\"96\" id=\"Part_96\" type=\"checkbox\" /></div>"
}]

var res = {};

$.each(arr, function(i, el) {
    if (!res[el.versionID] || res[el.versionID].versionNR < el.versionNR) {
        res[el.versionID] = el;
    }
});

var ret = [];
$.each(res,function(k,v){
  ret.push(v);
});

console.log(ret);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

答案 2 :(得分:0)

var uni = [];
for (var i = 0; i < arr.length; i++) {
    var find = false;
    for (var k=0; k < uni.length; k++) {
        if (arr[i]['versionID'] == uni[k]['versionID']) {
            if (arr[i]['versionNR'] > uni[k]['versionNR']) {
                uni[k]['versionNR'] = arr[i]['versionNR'];
                uni[k]['format'] = arr[i]['format'];
            }
            find = true;
        }
    }
    if (find == false) {
        uni.push(arr[i]);
    }
}

数组uni中的结果是