使用数组对象获取最大的更大键值

Get maximum greater key values with object of array

我想把一个对象的最大key放到Javascript的数组中,下面是JSON数组的例子。我尝试使用 reduce() ES6 函数,但它只会 return 记录,所以请帮助我获得最大编号。键数组,我还提供了我想要的输出,如果高阶函数(ES6)中的解决方案会很棒

let arr = [{
                key : 1,
                name : 'testaa',
                dept : 'ggg'
            }, {
                key : 1,
                name : 'testaa',
                dept : 'ggg'
            }, {
                key : 2,
                name : 'testaa',
                dept : 'ggg'
            }, {
                key : 2,
                name : 'testaa',
                dept : 'ggg'
            }, {
                key : 2,
                name : 'testaa',
                dept : 'ggg'
            }, {
                key : 3,
                name : 'testaa',
                dept : 'ggg'
            }, {
                key : 3,
                name : 'testaa',
                dept : 'ggg'
            }]

 output i want maximum key of array:


    arr = [{
                key : 3,
                name : 'testaa',
                dept : 'ggg'
            }, {
                key : 3,
                name : 'testaa',
                dept : 'ggg'
            }]

我尝试使用 reduce 函数,但只得到一条记录

let data = myArray.reduce(function(prev, curr) {
    return prev.key > curr.key ? prev : curr;
});

您只是返回了最后一个更高的键。您必须构建一个包含所有具有较高键的元素的数组。

在我的算法中,我将最高键存储在一个数组中,当我遇到一个键高于我存储的元素的元素时,我将数组重新创建一个。

const arr = [{
  key: 1,
  name: 'testaa',
  dept: 'ggg'
}, {
  key: 1,
  name: 'testaa',
  dept: 'ggg'
}, {
  key: 2,
  name: 'testaa',
  dept: 'ggg'
}, {
  key: 2,
  name: 'testaa',
  dept: 'ggg'
}, {
  key: 2,
  name: 'testaa',
  dept: 'ggg'
}, {
  key: 3,
  name: 'testaa',
  dept: 'ggg'
}, {
  key: 3,
  name: 'testaa',
  dept: 'ggg'
}];

const higherKey = arr.reduce((tmp, x) => {
  if (!tmp.length || tmp[0].key < x.key) {
    return [x];
  }

  if (tmp[0].key === x.key) {
    tmp.push(x);
  }

  return tmp;
}, []);

console.log(higherKey);

您可以分两步完成:

  1. 使用Math.max
  2. 找到最大值
  3. 使用 .filter()
  4. 使用此值过滤您的数组

let arr = [{
  key: 1,
  name: 'testaa',
  dept: 'ggg'
}, {
  key: 1,
  name: 'testaa',
  dept: 'ggg'
}, {
  key: 2,
  name: 'testaa',
  dept: 'ggg'
}, {
  key: 2,
  name: 'testaa',
  dept: 'ggg'
}, {
  key: 2,
  name: 'testaa',
  dept: 'ggg'
}, {
  key: 3,
  name: 'testaa',
  dept: 'ggg'
}, {
  key: 3,
  name: 'testaa',
  dept: 'ggg'
}];

let max = Math.max(...arr.map(item => item.key));

console.log(arr.filter(item => item.key === max));

let arr = [{
    key : 1,
    name : 'testaa',
    dept : 'ggg'
}, {
    key : 1,
    name : 'testaa',
    dept : 'ggg'
}, {
    key : 3,
    name : 'testaa',
    dept : 'ggg'
}, {
    key : 2,
    name : 'testaa',
    dept : 'ggg'
}, {
    key : 2,
    name : 'testaa',
    dept : 'ggg'
}, {
    key : 8,
    name : 'testaa',
    dept : 'ggg'
}, {
    key : 3,
    name : 'testaa',
    dept : 'ggg'
}]
let max = arr[0];
let data = arr.forEach(function(curr,index) {
if(max.key < curr.key) {
max = curr;
}
});
result = arr.map((item) => {
return item.key === map.key;
});
console.log(result)

我建议使用两个循环,一个用于找出最大密钥,然后过滤这些密钥,复杂度为 o(n)

如果要用reduce,在单次迭代中,可以这样用(真的很啰嗦,想简化的话可以):

let data = arr.reduce(function(acc, curr) {
    // If there is not data on the accumulator, add the first element
    if (acc.length === 0) {
        acc.push(curr);
        return acc;
    }
    // if current key is smaller than the stored one, clear and start a new accumulator
    if (acc[0].key < curr.key) {
        acc = [];
        acc.push(curr);
    }
    // If key is the same than the stored one, add it to the accumulator
    else if(acc[0].key === curr.key) {
        acc.push(curr);
    }

    // Return the accumulator
    return acc;
}, []);