获取包含对象数组的最大键值。

3
我想在Javascript中将对象的最大键插入到数组中,以下是一个JSON数组的示例。我尝试使用reduce()ES6函数,但它只会返回一个记录,因此请帮助我获得最大键数组,我也提供了所需的输出,如果解决方案在高阶函数(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;
});

4
“{ key = 1, name = 'testaa', dept = 'ggg' }” 的语法不正确,“=” 应该改成 “:”。 - Maheer Ali
reduce 只会返回一个值.....而且你没有设置 reducer 的默认值来开始.... - epascarello
你说你想要最大的键。你的reduce调用返回了最大的键。那么当你说“只有一个”时,你还想要什么? - idmean
4个回答

4
您可以分两步完成:
  1. 使用Math.max找到最大值。
  2. 使用.filter()方法,将数组中小于该最大值的元素过滤出来。

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));


Serge K,感谢您的解决方案,我同意您的解决方案,但是您正在迭代两次,一次用于map(),另一次用于filter(),我们可以在reduce()函数中完成这个操作,只需要一次迭代。 - sarvesh kumar
1
@sarveshkumar 请查看Gregory NEUT的回答 - Serge K.

3

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

在我的算法中,我将最高键存储在一个数组中,当我遇到一个具有比我存储的元素更高的键的元素时,我会清除并重新创建一个数组。

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);


0
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)。

0
如果你想使用 `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;
}, []);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接