简体   繁体   English

javascript数组找到最高价值

[英]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. 我有一个数组,我需要找到最高值,所以当我将它应用于Highchart我可以为背景着色,如果它超过用户输入的动态数字。

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( {} ). 您的array变量不是数组( [] ),它是一个对象( {} )。 You have multiple items in the object with the key data , but only one value per key is allowed. 对象中有多个项目包含密钥data ,但每个密钥只允许一个值。

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. 或完全摆脱data ,只是使它成为一个数组数组。

First make a proper nested array set. 首先制作一个合适的嵌套数组。 Then you can use .map() to do this: 然后你可以使用.map()来做到这一点:

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 试试这个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. JavaScript中的对象文字需要唯一的键。 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). 您正在使用具有相同名称的多个密钥,这些密钥不是有效的JavaScript(嗯,代码可能仍在运行,但您无法获得您期望的结果)。 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. 然而,这可能是为了在生产代码中使用混淆,我宁愿使用reduce代替更好的可读性。

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: 最后,您还可以使用sort方法替换后者reduce:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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