admin管理员组文章数量:1431498
I have an array like
var arr = [ [1, 4, 5],
[2, 6, 7],
[3, 3, 9]]
I would like to get the row which has max value in column 2, so in this example row 2. How can I do this in javascript?
Edit: I can do it using a for loop which iterates over all rows and have a temp max variable to keep track of the max. But I was hoping for a more efficient way.
Thanks
I have an array like
var arr = [ [1, 4, 5],
[2, 6, 7],
[3, 3, 9]]
I would like to get the row which has max value in column 2, so in this example row 2. How can I do this in javascript?
Edit: I can do it using a for loop which iterates over all rows and have a temp max variable to keep track of the max. But I was hoping for a more efficient way.
Thanks
Share Improve this question asked Feb 25, 2014 at 12:20 dopplesoldnerdopplesoldner 9,53912 gold badges46 silver badges57 bronze badges 5- 2 Did you try something ? Like for example iterate over the array ? – Denys Séguret Commented Feb 25, 2014 at 12:21
- Yes I can do it using a for loop which iterates over all rows and have a temp max variable to keep track of the max. But I was hoping for a more efficient way – dopplesoldner Commented Feb 25, 2014 at 12:22
- 2 That's the most efficient way. Other ones are just sweeter. – Denys Séguret Commented Feb 25, 2014 at 12:23
- What would you like to have happen in the case of a tie? – Will Commented Feb 25, 2014 at 12:31
- @Will in case of a tie, the last found element will be used. – dopplesoldner Commented Feb 25, 2014 at 12:32
8 Answers
Reset to default 3Well, iterating and keeping a temporary max variable is probably the most efficient way of doing it, but if you want a way that looks more pleasing, you can try something like:
var col2 = arr.map(function (elem) {
return elem[1]; //to get all the column 2 values
});
var index = col2.indexOf(Math.max.apply(this, col2));
Edit: If you want to use the index of the last found element in case of a tie, use
col2.lastIndexOf(Math.max.apply(this, col2));
The below code will give you the highest number:
var array = [[1, 4, 5],
[2, 6, 7],
[3, 3, 9]];
var bigNum = 0;
for(var i=0;i<array.length; i++){
var largest = Math.max.apply(Math, array[i]);
if(largest > bigNum) {
bigNum = largest;
}
}
console.log(bigNum);
How about this
var arr = [[1, 4, 5], [2, 6, 7], [3, 3, 9]],
t;
$.each(arr, function(k, v){
t = !t ? v[1] : (v[1] > t ? v[1] : t);
});
console.log(t);
Nothing can out better plain loop version, but you can use Array.prototype.reduce
like this
var arr = [ [1, 4, 5], [2, 6, 7], [3, 3, 9]], col = 1;
var best = arr.reduce(function(tillNow, current) {
if (tillNow.best < current[col]) {
tillNow.best = current[col];
tillNow.row = current;
}
return tillNow;
}, {best:0, row:[]});
console.log(best.row); # [ 2, 6, 7 ]
console.log(best); # { best: 6, row: [ 2, 6, 7 ] }
Reduce function accepts the till now value as the first parameter and the current element as the second parameter.
For the first element, parameters will be like this
tillNow : {best:0, row:[]} : current: [1, 4, 5]
We pare current
's indented column with tillNow.best
. If current's is bigger than tillNow
, the best element and the current row will be set in tillNow
and that will be returned which will be fed back into the reduce
's next iteration as tillNow
. So, in our case, on the second iteration, values change like this
tillNow : {best:4, row: [1, 4, 5]} : current: [2, 6, 7]
And on third iteration,
tillNow : {best:6, row: [2, 6, 7]} : current: [3, 3, 9]
And finally the result will be
{ best: 6, row: [ 2, 6, 7 ] }
Please try this.
var col2 = [];
var max = 0;
$.each(arr, function(i, val){
col2.push(val[1]);
})
max = Math.max.apply( Math, col2 );
This example returns runs map
over over each row and returns an array with the each row's number from the nominated column. It then returns the index of the highest number in that array.
function findRowForMaxInCol(arr, col) {
var column = arr.map(function(el) { return el[col]; });
var highest = Math.max.apply(null, column);
return column.indexOf(highest);
}
console.log(findRowForMaxInCol(arr, 1)); // 1
Fiddle.
Just iterating through the array and checking if the second element is largest. The variable large
will contain the row i
and the largest value e
.
var arr = [ [1, 4, 5],
[2, 6, 7],
[3, 3, 9]],
large = {i:0, e: 0}; // i: row, e: largest elem
arr.map(function (a, idx) {
if (a[1] > large.e) {
large.e = a[1];
large.i = idx;
}
});
console.log(large); // {i: 1, e: 6}. row: 1 and element 6.
Generalizing the problem: find the element in an array which has the maximum 'f(x)':
var findMax = function(array, f){
var element = null;
var max = -Infinity;
for (var i=0; i!=array.length; ++i){
var value = f(array[i]);
if (value > max){
element=array[i];
max=value;
}
};
return element;
};
And apply the general algorithm to rows
with a function returning the second element of the row:
var secondElement= function(row){ return row[1]; };
var max = findMax(rows, secondElement);
(JSFiddle)
本文标签: javascriptget row with max value in multi dimesion arrayStack Overflow
版权声明:本文标题:javascript - get row with max value in multi dimesion array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745582105a2664681.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论