Javascript 在矩阵中查找重复的最大值

Javascript 在矩阵中查找重复的最大值,javascript,arrays,algorithm,array-algorithms,Javascript,Arrays,Algorithm,Array Algorithms,我使用的是一个对象矩阵,我试图找到每个对象的最大值,包括重复项 以下是我到目前为止所拥有的: let findColumnMaxValue = (i) => { let coord = []; let maxValue = 0; for (let j = 0; j < this.field.length; j++) { if (this.field[i][j].dst > maxVal

我使用的是一个对象矩阵,我试图找到每个对象的最大值,包括重复项

以下是我到目前为止所拥有的:

        let findColumnMaxValue = (i) => {
         let coord = [];
         let maxValue = 0;
         for (let j = 0; j < this.field.length; j++) {
            if (this.field[i][j].dst > maxValue) {
                maxValue = this.field[i][j].dst;
            }
        }

        getMaxValueCoord(maxValue, coord, i);
        return coord;
    }

期望的结果是获得矩阵中的所有最大值作为坐标,包括重复项。在上面的示例中,这将是[9,9,9,9]。

使用、、、检查一些魔法:

const矩阵=[
[{dst:2}、{dst:3}、{dst:4}、{dst:5}、{dst:6}、{dst:5}、{dst:4}、{dst:3}、{dst:2}],
[{dst:3}、{dst:4}、{dst:5}、{dst:6}、{dst:7}、{dst:6}、{dst:5}、{dst:4}、{dst:3}],
[{dst:4}、{dst:5}、{dst:6}、{dst:7}、{dst:8}、{dst:7}、{dst:6}、{dst:5}、{dst:4}],
[{dst:5}、{dst:6}、{dst:3}、{dst:4}、{dst:9}、{dst:4}、{dst:3}、{dst:2}、{dst:1}],
[{dst:6}、{dst:7}、{dst:3}、{dst:4}、{dst:9}、{dst:4}、{dst:3}、{dst:2}、{dst:1}],
[{dst:6}、{dst:7}、{dst:3}、{dst:4}、{dst:5}、{dst:4}、{dst:3}、{dst:2}、{dst:1}],
[{dst:5}、{dst:6}、{dst:3}、{dst:4}、{dst:5}、{dst:5}、{dst:4}、{dst:3}、{dst:2}、{dst:1}],
[{dst:4}、{dst:6}、{dst:3}、{dst:4}、{dst:5}、{dst:4}、{dst:3}、{dst:2}、{dst:1}],
[{dst:3}、{dst:5}、{dst:3}、{dst:4}、{dst:5}、{dst:4}、{dst:3}、{dst:2}、{dst:1}],
[{dst:2}、{dst:4}、{dst:3}、{dst:4}、{dst:5}、{dst:4}、{dst:3}、{dst:2}、{dst:1}],
];
const maxArray=matrix.reduce((maxArray,row,rowIndex)=>{
const max=Math.max(0,…row.map(e=>e.dst));
返回maxArray.concat(
row.map(
(e,i)=>({x:i,y:rowIndex,dst:e.dst})
).filter(e=>e.dst==max)
);
}, []);
const maxOfAll=Math.max(0,…maxArray.map(e=>e.dst));
常量filteredMaxArray=maxArray.filter(
e=>e.dst==maxOfAll
).map(e=>({x:e.x,y:e.y}));

日志(filteredMaxArray)您可以使用单个函数,在外部数组和内部数组上仅使用一个循环,并使用哈希表作为临时最大坐标,使用收集数组作为结果

函数getMax(数据){
返回数据.reduce(函数(r,a,x){
var hash=Object.create(null),
max=0;
a、 forEach(函数(o,y){

如果(如果您只是想减少代码大小,请查看array.map、array.reduce或Math.max函数以帮助您。如果您可以添加多个数组的示例输入,其他人将更容易帮助您。您可以添加数据矩阵和所需结果吗?请添加一些数据和所需结果。为什么不使用此
Math、 max(max,…row)
?@SergeyDenisov重复的呢?@TudorApostol请检查,现在所有重复的都保存了。@SergeyDenisov真的很好,感谢您的努力。
        let getMaxValueCoord = (max, a, i) => {
         for (let j = 0; j < this.field.length; j++) {
            if (this.field[i][j].dst === max) {
                a.push({x: i, y: j})
            }
        }
    }
    findHighestDensityCells() {
     let arr = []; 
     for (let i = 0; i < this.field.length; i++) {
         arr.push(findColumnMaxValue(i));
     }

     return [].concat(...arr);
}
 2 3 4 5 6 6 5 4 3 2
 3 4 5 6 7 7 6 5 4 3
 4 5 6 7 8 8 7 6 5 4
 5 6 3 4 9 9 4 3 2 1
 6 7 3 4 9 9 4 3 2 1
 6 7 3 4 5 5 4 3 2 1
 5 6 3 4 5 5 4 3 2 1
 4 6 3 4 5 5 4 3 2 1
 3 5 3 4 5 5 4 3 2 1
 2 4 3 4 5 5 4 3 2 1
const maxArray = matrix.reduce((maxArray, row, rowIndex) => {
  const max = Math.max(0, ...row.map(e => e.dst));

  return maxArray.concat(
    row.map(
      (e, i) => ({x: i, y: rowIndex, dst: e.dst})
    ).filter(e => e.dst === max)
  );
}, []);

const maxOfAll = Math.max(0, ...maxArray.map(e => e.dst));

const filteredMaxArray = maxArray.filter(
  e => e.dst === maxOfAll
).map(e => ({x: e.x, y: e.y}));