简体   繁体   中英

javascript array find highest value

I have an array that I need to find the highest value of so when I apply it to a Highchart I can color the background if it exceeds some dynamic number a user inputs.

My code looks like this:

<div id="highest"></div>

var array = {
    data: [123, 234, 456, 789],
    data: [321, 654, 987],
    data: [963, 852, 741]
};

for (var i=0; i < array.data.length; i++){
    var m = Math.max.apply(Math, array.data);
    $('#highest').append('<div>'+m+'</div>');
}

all I get is the number

<div>963</div>
<div>963</div>
<div>963</div>

Your array variable isn't an array( [] ), it is an object( {} ). You have multiple items in the object with the key data , but only one value per key is allowed.

So you have essentially written this:

var array = {
  data: [963, 852, 741]
};

Maybe you want something like this?

var array = [
  {data: [123, 234, 456, 789]},
  {data: [321, 654, 987]},
  {data: [963, 852, 741]}
];

var values = [];
for (var i=1; i < array.length; i++) {
  values.push.apply(values, array[i].data);
}
$('#highest').append('<div>' + Math.max.apply(Math, values) + '</div>');

or get rid of data entirely and just make it an array of arrays.

First make a proper nested array set. Then you can use .map() to do this:

var array = [
    [123, 234, 456, 789],
    [321, 654, 987],
    [963, 852, 741]
];

var maxVals = array.map(function(a) {
    return Math.max.apply(Math, a);
});

Or like this:

var maxVals = array.map(Function.apply.bind(Math.max, Math));

Well, that's not an array, it's an object with duplicated keys so you're just looping the last one that overrides all others.

Try this http://jsbin.com/olahuq/2/edit

var obj = {
  a: [123, 234, 456, 789],
  b: [321, 654, 987],
  c: [963, 852, 741]
};

var max = [];
for (var i in obj) {
  max.push(Math.max.apply(0, obj[i]));
}

$('#highest').append('<div>'+ max.join('</div><div>') +'<div>');

Altough you could just use an array of objects.

Not sure what you're trying to do here. That "array" variable is defined as an object. Object literals in JavaScript need unique keys. Your last array keyed by "data" in the "array" object is the only one that the interpreter picks up.

You might want this?

var data = [
  [123, 234, 456, 789],
  [321, 654, 987],
  [963, 852, 741]
];

for (var i=0; i < data.length; i++){
  var m = Math.max.apply(Math, data[i]);
  $('#highest').append('<div>'+m+'</div>');
}

You are using multiple keys with the same name, which isn't valid JavaScript (well, the code may still run, but you won't get the results you're expecting). This is easy to fix, though—just use an array instead:

var arr = [
    [123, 234, 456, 789],
    [321, 654, 987],
    [963, 852, 741]
];

for (var i=0; i < arr.length; i++){
    var m = Math.max.apply(Math, arr[i]);
    $('#highest').append('<div>'+m+'</div>');
}

You could do this with an object, but I don't see any reason to, unless you're reading this data from a service or something and can't change its format.

I just want to add another even more reduced way.

var obj = {
  a: [123, 234, 456, 789],
  b: [321, 654, 987],
  c: [963, 852, 741]
};
var max = Math.max.apply(0, [].concat.apply([], Object.values(obj)));

This however might be to obfuscated to be used in production code, I'd rather use reduce instead for better readability.

var max = Object.values(obj)
      .reduce((a, d) => a.concat(d), [])
      .reduce((p, c) => Math.max(p, c));

Finally you can also replace the latter reduce with a sort method:

var max = Object.values(obj)
      .reduce((a, d) => a.concat(d), [])
      .sort((l, r) => l - r)
      .pop();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM