admin管理员组文章数量:1317572
how to get maximum of value between multiple keys from array?
I have tried this below method for only three(not multiple) keys.
getMaxValuefromkeys(values: any[], key1: string, key2: string, key3: string) {
var val1 = Math.max.apply(Math, values.map(function (a) { return a[key1] }));
var val2 = Math.max.apply(Math, values.map(function (a) { return a[key2]; }));
var val3 = Math.max.apply(Math, values.map(function (a) { return a[key2]; }));
if (val1 >= val2 || val1 >= val3) {
return val1;
} else if (val2 >= val3 || val2 >= val1) {
return val2;
}
return val3;
}
But we need to check more condition and write more codes if we use multiple keys. So I have tried these below codes
Math.max.apply(Math, values.map(function (a) { return a[key1], a[key2], a[key3]; }));
// where I want to return multiple keys
But it's not working. Is any single line of code available for getting max value in between multiple keys from array?
how to get maximum of value between multiple keys from array?
I have tried this below method for only three(not multiple) keys.
getMaxValuefromkeys(values: any[], key1: string, key2: string, key3: string) {
var val1 = Math.max.apply(Math, values.map(function (a) { return a[key1] }));
var val2 = Math.max.apply(Math, values.map(function (a) { return a[key2]; }));
var val3 = Math.max.apply(Math, values.map(function (a) { return a[key2]; }));
if (val1 >= val2 || val1 >= val3) {
return val1;
} else if (val2 >= val3 || val2 >= val1) {
return val2;
}
return val3;
}
But we need to check more condition and write more codes if we use multiple keys. So I have tried these below codes
Math.max.apply(Math, values.map(function (a) { return a[key1], a[key2], a[key3]; }));
// where I want to return multiple keys
Share Improve this question edited Jun 23, 2017 at 11:18 Ramesh Rajendran asked Jun 23, 2017 at 10:59 Ramesh RajendranRamesh Rajendran 38.7k49 gold badges157 silver badges239 bronze badges 4But it's not working. Is any single line of code available for getting max value in between multiple keys from array?
- Kindly explain who's/why down voted for this question. :( – Ramesh Rajendran Commented Jun 23, 2017 at 11:28
- 2 I don't know if this is the reason, but perhaps the question seems basic for a user of your reputation. I'm not saying that it is, or that it's a legit reason for downvoting, only that from my experience sometimes downvotes, in as much as some of them make sense, are on occasion related to reputation in relation to content, as well as just content itself. (I did not downvote and I very rarely do, but I know that others are less reticent). Or maybe someone lost their keys (I don't find this reason for voting funny, but I know it's munity accepted as being funny). – Peter Carter Commented Jun 23, 2017 at 11:36
- @PeterDavidCarter ! it's a large ment than below answers :p – Ramesh Rajendran Commented Jun 23, 2017 at 11:57
- 1 Yeah, it's probably a good question in that it's got a lot of answers so probably produced useful content for the munity. I think it's fine. meta.stackexchange./questions/215379/… contains the answer I was refering to when referencing lost keys. (My formal training was first in literature so I have a tendency to ramble on ;)). – Peter Carter Commented Jun 23, 2017 at 11:59
3 Answers
Reset to default 7Array#map
each object to it's maximum value, then find the maximum of the array:
var values = [{ a: 4, b: 3 }, { a: 2, b: 8 }, { a: 1, b: 2 }];
var key1 = 'a', key2 = 'b';
var result = Math.max.apply(Math, values.map(function (a) { return Math.max(a[key1], a[key2]); }));
console.log(result);
If you want something more flexible that can accept multiple keys:
var values = [{ a: 4, b: 3, c: 23 }, { a: 2, b: 28, c: 13 }, { a: 1, b: 2, c: 1 }];
function getMaxOfKeys(values, keys) {
return Math.max.apply(Math, values.map(function(obj) {
return Math.max.apply(Math, keys.map(function(key) {
return obj[key];
}));
}));
}
// or the ES6 equivalent
const getMaxOfKeysES6 = (values, keys) =>
Math.max(...values.map(
(obj) =>
Math.max(...keys.map((key) => obj[key]))
)
);
console.log(getMaxOfKeys(values, ['a', 'b', 'c']));
console.log(getMaxOfKeysES6(values, ['a', 'b', 'c']));
(plain ES6 - I don't do TypeScript)
Given your object values
and a set of keys
that you wish to pare, e.g.:
let values = [
{ key1: 2, key2: 3, key3: 0 },
{ key1: 10, key2: 0, key3: 5 }
];
then for each key, find the maximal value within each object and return that as an array, and then find the maximal value of that array.
function getMaxValuefromkeys(values, ...keys) {
return Math.max(...keys.map(key => Math.max(...values.map(o => o[key]))));
}
with usage:
let max = getMaxValuefromkeys(values, 'key1', 'key2', 'key3');
or given an array of keys:
let max = getMaxValuefromkeys(values, ...keys);
Math.max.apply(Math, values.map(function (a) { return Math.max(a[key1],a[key2]) }));
Note that
return a,b;
Is the ma operator , so actually it just returns b.
本文标签: javascripthow to get maximum of value between multiple keys from arrayStack Overflow
版权声明:本文标题:javascript - how to get maximum of value between multiple keys from array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742020128a2414455.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论