Getting the lowest value with key in array The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceget key from a javascript object with minimum valueCreate ArrayList from arrayHow do I check if an array includes an object in JavaScript?How to append something to an array?Checking if a key exists in a JavaScript object?Sort array of objects by string property valueLoop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?Copy array by valueFor-each over an array in JavaScript?
Wolves and sheep
Sort a list of pairs representing an acyclic, partial automorphism
He got a vote 80% that of Emmanuel Macron’s
system() function string length limit
Why did all the guest students take carriages to the Yule Ball?
In horse breeding, what is the female equivalent of putting a horse out "to stud"?
Would an alien lifeform be able to achieve space travel if lacking in vision?
Are my PIs rude or am I just being too sensitive?
Is above average number of years spent on PhD considered a red flag in future academia or industry positions?
Is there a writing software that you can sort scenes like slides in PowerPoint?
Match Roman Numerals
What aspect of planet Earth must be changed to prevent the industrial revolution?
Python - Fishing Simulator
Who or what is the being for whom Being is a question for Heidegger?
What's the point in a preamp?
Does Parliament hold absolute power in the UK?
Can a 1st-level character have an ability score above 18?
How do you keep chess fun when your opponent constantly beats you?
Do warforged have souls?
Did God make two great lights or did He make the great light two?
University's motivation for having tenure-track positions
Typeface like Times New Roman but with "tied" percent sign
Relations between two reciprocal partial derivatives?
When did F become S in typeography, and why?
Getting the lowest value with key in array
The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experienceget key from a javascript object with minimum valueCreate ArrayList from arrayHow do I check if an array includes an object in JavaScript?How to append something to an array?Checking if a key exists in a JavaScript object?Sort array of objects by string property valueLoop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?Copy array by valueFor-each over an array in JavaScript?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have an array like this: arr = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, …
And I'm trying get the lowest value with key using Javascript.
What I've tried:
alert(Math.min.apply(Math, arr));
returns Infinity
I don't know why
I got this on Google, just for try:
var keys = Object.keys(arr).map(Number).filter(function(a)
return arr[a];
); alert(Math.min.apply(Math, keys));
returns Infinity
too
I want something more complete, like this output: "The lowest value is 2 from lst9".
I really tried fix it myself before asking here, but without success!
Can you help me fix this "Infinity" issue? Thank you.
javascript arrays object
|
show 4 more comments
I have an array like this: arr = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, …
And I'm trying get the lowest value with key using Javascript.
What I've tried:
alert(Math.min.apply(Math, arr));
returns Infinity
I don't know why
I got this on Google, just for try:
var keys = Object.keys(arr).map(Number).filter(function(a)
return arr[a];
); alert(Math.min.apply(Math, keys));
returns Infinity
too
I want something more complete, like this output: "The lowest value is 2 from lst9".
I really tried fix it myself before asking here, but without success!
Can you help me fix this "Infinity" issue? Thank you.
javascript arrays object
What is expected output?
– Maheer Ali
Mar 25 at 6:45
3
Do you want to get, for example,lst3
ifobj.lst3
is the lowest value?
– Jack Bashford
Mar 25 at 6:47
3
Also, what islst9
? Is itlst9: 2
?
– Jack Bashford
Mar 25 at 6:48
may be a duplicte of stackoverflow.com/questions/38008307/…
– Syed Mehtab Hassan
Mar 25 at 6:49
10
arr
is not an array but anobject
– Isaac
Mar 25 at 6:53
|
show 4 more comments
I have an array like this: arr = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, …
And I'm trying get the lowest value with key using Javascript.
What I've tried:
alert(Math.min.apply(Math, arr));
returns Infinity
I don't know why
I got this on Google, just for try:
var keys = Object.keys(arr).map(Number).filter(function(a)
return arr[a];
); alert(Math.min.apply(Math, keys));
returns Infinity
too
I want something more complete, like this output: "The lowest value is 2 from lst9".
I really tried fix it myself before asking here, but without success!
Can you help me fix this "Infinity" issue? Thank you.
javascript arrays object
I have an array like this: arr = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, …
And I'm trying get the lowest value with key using Javascript.
What I've tried:
alert(Math.min.apply(Math, arr));
returns Infinity
I don't know why
I got this on Google, just for try:
var keys = Object.keys(arr).map(Number).filter(function(a)
return arr[a];
); alert(Math.min.apply(Math, keys));
returns Infinity
too
I want something more complete, like this output: "The lowest value is 2 from lst9".
I really tried fix it myself before asking here, but without success!
Can you help me fix this "Infinity" issue? Thank you.
javascript arrays object
javascript arrays object
edited Mar 25 at 20:38
Jack Bashford
16.3k41848
16.3k41848
asked Mar 25 at 6:44
MerntMernt
845
845
What is expected output?
– Maheer Ali
Mar 25 at 6:45
3
Do you want to get, for example,lst3
ifobj.lst3
is the lowest value?
– Jack Bashford
Mar 25 at 6:47
3
Also, what islst9
? Is itlst9: 2
?
– Jack Bashford
Mar 25 at 6:48
may be a duplicte of stackoverflow.com/questions/38008307/…
– Syed Mehtab Hassan
Mar 25 at 6:49
10
arr
is not an array but anobject
– Isaac
Mar 25 at 6:53
|
show 4 more comments
What is expected output?
– Maheer Ali
Mar 25 at 6:45
3
Do you want to get, for example,lst3
ifobj.lst3
is the lowest value?
– Jack Bashford
Mar 25 at 6:47
3
Also, what islst9
? Is itlst9: 2
?
– Jack Bashford
Mar 25 at 6:48
may be a duplicte of stackoverflow.com/questions/38008307/…
– Syed Mehtab Hassan
Mar 25 at 6:49
10
arr
is not an array but anobject
– Isaac
Mar 25 at 6:53
What is expected output?
– Maheer Ali
Mar 25 at 6:45
What is expected output?
– Maheer Ali
Mar 25 at 6:45
3
3
Do you want to get, for example,
lst3
if obj.lst3
is the lowest value?– Jack Bashford
Mar 25 at 6:47
Do you want to get, for example,
lst3
if obj.lst3
is the lowest value?– Jack Bashford
Mar 25 at 6:47
3
3
Also, what is
lst9
? Is it lst9: 2
?– Jack Bashford
Mar 25 at 6:48
Also, what is
lst9
? Is it lst9: 2
?– Jack Bashford
Mar 25 at 6:48
may be a duplicte of stackoverflow.com/questions/38008307/…
– Syed Mehtab Hassan
Mar 25 at 6:49
may be a duplicte of stackoverflow.com/questions/38008307/…
– Syed Mehtab Hassan
Mar 25 at 6:49
10
10
arr
is not an array but an object
– Isaac
Mar 25 at 6:53
arr
is not an array but an object
– Isaac
Mar 25 at 6:53
|
show 4 more comments
9 Answers
9
active
oldest
votes
You can get the key and value using Object.entries
:
var arr =
lst1: 300,
lst2: 381,
lst3: 4,
lst4: 4,
lst5: 49
;
function lowestValueAndKey(obj)
var lowestItems = Object.entries(obj).sort(([ ,v1], [ ,v2]) => v1 - v2)[0];
return `Lowest value is $lowestItems[1], with a key of $lowestItems[0]`;
var lowest = lowestValueAndKey(arr);
console.log(lowest);
1
Hello, Are there an way to make this script compatible with Internet Explorer too?
– Mernt
Mar 25 at 7:41
@Mernt developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– JollyJoker
Mar 25 at 7:59
I tried but I'm getting a syntax error on linevar lowestItems = Object.entries(arr).sort(([ ,v1], [ ,v2]) => v1 - v2)[0]
– Mernt
Mar 25 at 8:06
If you look here developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… you'll see that Internet Explorer does not supportObject.entries
– ChatterOne
Mar 25 at 8:08
2
@Mernt IE doesn't support the fat arrow notation. You need to rewrite([ ,v1], [ ,v2]) => v1 - v2
in old style Javascript
– JollyJoker
Mar 25 at 8:12
|
show 5 more comments
There are several ways to arrive at what you want. Please see the following methods:
const obj = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49
const values = Object.values(obj)
const lowest = Math.min.apply(null, values)
// Using Object.keys(), Object.values() and findIndex()
const keys = Object.keys(obj)
const indexOfLowest = values.findIndex(function (x) return x === lowest )
console.log(`The lowest value is $lowest from $keys[indexOfLowest]`)
// OR Using Object.keys() and filter()
const key = Object.keys(obj).filter(function (x) return obj[x] === lowest )[0]
console.log(`The lowest value is $lowest from $key`)
// OR Using Object.entries() and sort()
const [[lowestKey, lowestVal]] = Object.entries(obj).sort(function ([,valA], [,valB]) return valA - valB );
console.log(`The lowest value is $lowestVal from $lowestKey`)
It should work as is in IE > = 11
– John Kennedy
Mar 25 at 8:02
Syntax error on linevar key = Object.keys(arr).filter(x => arr[x] === lowest)[0]
– Mernt
Mar 25 at 8:08
1
I'll suggest using a polyfill if it doesn't work in IE. See here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… I should also inform you that on my IE on Windows 10 it works without needing a polyfill.
– John Kennedy
Mar 25 at 8:10
1
@JohnKennedy I'd be surprised by that, IE doesn't support arrow functions
– Nick A
Mar 25 at 14:47
add a comment |
There are many ways to approach your problem, but here is one simple alternative way to do it. Basically, it is a brute force way of doing things, whereby you iterate through every property to check for the value.
const obj = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49;
const getSmallest = function (object)
let smallestValue = Number.MAX_VALUE;
let selectedKey = '';
for (let key in object)
if (object[key] < smallestValue)
smallestValue = object[key];
selectedKey = key;
;
console.log(selectedKey, smallestValue)
return `The lowest value is $smallestValue from $selectedKey`;
;
getSmallest(obj);
add a comment |
To get only the minimum number you can use Math.min(...Object.entries(arr).map(o => o[1]))
:
const arr = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49;
const lowestValue = Math.min(...Object.entries(arr).map(o => o[1]));
console.log(lowestValue);
And to get the object with the minimum value you can use Object.entries(arr).reduce((a, [k, v]) => a[Object.keys(a)[0]] < v ? a : [k]: v, )
:
const arr = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49;
const lowestObj = Object.entries(arr).reduce((a, [k, v]) => a[Object.keys(a)[0]] < v ? a : [k]: v, );
console.log(lowestObj);
add a comment |
Here we first assume that the min, key and value as first property and the value respectively. Then we iterate through all the elements and compare with minValue to find any value less than that in the object. If it is, then we update both minValue and minKey. Anyway, the arr type is an Object. it is not an array in javascript.
var arr = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49;
var minkey = Object.keys(arr)[0];
var minValue = arr[Object.keys(arr)[0]];
for (var key in arr)
if (arr.hasOwnProperty(key))
if(arr[key] < minValue)
minValue = arr[key];
minKey = key;
add a comment |
You can try this:
obj = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2; // This is an object, That;s ehy you getting infinite
var arr = Object.keys( obj ).map(function ( key ) return obj[key]; );
alert(Math.min.apply( null, arr ));
Fiddle: https://jsfiddle.net/ozvubmdr/
add a comment |
Easy, though not optimal, as it traverses the array twice: find the smallest value, then find the key belonging to it.
arr = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2
min = Math.min(...Object.values(arr));
minkey = Object.keys(arr).find(key => arr[key] = min);
console.log(`Lowest is $min at $minkey`);
add a comment |
You may try this:
const arr = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2
const minVal = Math.min(...Object.values(arr))
const fromKey = Object.keys(arr).find(key => arr[key] === minVal)
console.log(`The lowest value is $minVal from $fromKey`)
add a comment |
You can use Object.entries
and use Array.reduce
to reduce the resulting [key-value] pair to an object which will have lowest entry:
const data = lst1: 300, lst2: -381, lst3: 4, lst4: 4, lst5: 49, lst6: -49, lst7: 0, lst7: 78, lst7: -90;
const obj = Object.entries(data).reduce((acc, [key, value]) =>
acc["lowest"] = acc["lowest"] ? Object.values(acc["lowest"])[0] < value ? acc["lowest"] : [key] : value : [key] : value ;
return acc;
, );
console.log(`Lowest value is found in $JSON.stringify(obj.lowest)`);
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55332453%2fgetting-the-lowest-value-with-key-in-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can get the key and value using Object.entries
:
var arr =
lst1: 300,
lst2: 381,
lst3: 4,
lst4: 4,
lst5: 49
;
function lowestValueAndKey(obj)
var lowestItems = Object.entries(obj).sort(([ ,v1], [ ,v2]) => v1 - v2)[0];
return `Lowest value is $lowestItems[1], with a key of $lowestItems[0]`;
var lowest = lowestValueAndKey(arr);
console.log(lowest);
1
Hello, Are there an way to make this script compatible with Internet Explorer too?
– Mernt
Mar 25 at 7:41
@Mernt developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– JollyJoker
Mar 25 at 7:59
I tried but I'm getting a syntax error on linevar lowestItems = Object.entries(arr).sort(([ ,v1], [ ,v2]) => v1 - v2)[0]
– Mernt
Mar 25 at 8:06
If you look here developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… you'll see that Internet Explorer does not supportObject.entries
– ChatterOne
Mar 25 at 8:08
2
@Mernt IE doesn't support the fat arrow notation. You need to rewrite([ ,v1], [ ,v2]) => v1 - v2
in old style Javascript
– JollyJoker
Mar 25 at 8:12
|
show 5 more comments
You can get the key and value using Object.entries
:
var arr =
lst1: 300,
lst2: 381,
lst3: 4,
lst4: 4,
lst5: 49
;
function lowestValueAndKey(obj)
var lowestItems = Object.entries(obj).sort(([ ,v1], [ ,v2]) => v1 - v2)[0];
return `Lowest value is $lowestItems[1], with a key of $lowestItems[0]`;
var lowest = lowestValueAndKey(arr);
console.log(lowest);
1
Hello, Are there an way to make this script compatible with Internet Explorer too?
– Mernt
Mar 25 at 7:41
@Mernt developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– JollyJoker
Mar 25 at 7:59
I tried but I'm getting a syntax error on linevar lowestItems = Object.entries(arr).sort(([ ,v1], [ ,v2]) => v1 - v2)[0]
– Mernt
Mar 25 at 8:06
If you look here developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… you'll see that Internet Explorer does not supportObject.entries
– ChatterOne
Mar 25 at 8:08
2
@Mernt IE doesn't support the fat arrow notation. You need to rewrite([ ,v1], [ ,v2]) => v1 - v2
in old style Javascript
– JollyJoker
Mar 25 at 8:12
|
show 5 more comments
You can get the key and value using Object.entries
:
var arr =
lst1: 300,
lst2: 381,
lst3: 4,
lst4: 4,
lst5: 49
;
function lowestValueAndKey(obj)
var lowestItems = Object.entries(obj).sort(([ ,v1], [ ,v2]) => v1 - v2)[0];
return `Lowest value is $lowestItems[1], with a key of $lowestItems[0]`;
var lowest = lowestValueAndKey(arr);
console.log(lowest);
You can get the key and value using Object.entries
:
var arr =
lst1: 300,
lst2: 381,
lst3: 4,
lst4: 4,
lst5: 49
;
function lowestValueAndKey(obj)
var lowestItems = Object.entries(obj).sort(([ ,v1], [ ,v2]) => v1 - v2)[0];
return `Lowest value is $lowestItems[1], with a key of $lowestItems[0]`;
var lowest = lowestValueAndKey(arr);
console.log(lowest);
var arr =
lst1: 300,
lst2: 381,
lst3: 4,
lst4: 4,
lst5: 49
;
function lowestValueAndKey(obj)
var lowestItems = Object.entries(obj).sort(([ ,v1], [ ,v2]) => v1 - v2)[0];
return `Lowest value is $lowestItems[1], with a key of $lowestItems[0]`;
var lowest = lowestValueAndKey(arr);
console.log(lowest);
var arr =
lst1: 300,
lst2: 381,
lst3: 4,
lst4: 4,
lst5: 49
;
function lowestValueAndKey(obj)
var lowestItems = Object.entries(obj).sort(([ ,v1], [ ,v2]) => v1 - v2)[0];
return `Lowest value is $lowestItems[1], with a key of $lowestItems[0]`;
var lowest = lowestValueAndKey(arr);
console.log(lowest);
answered Mar 25 at 6:54
Jack BashfordJack Bashford
16.3k41848
16.3k41848
1
Hello, Are there an way to make this script compatible with Internet Explorer too?
– Mernt
Mar 25 at 7:41
@Mernt developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– JollyJoker
Mar 25 at 7:59
I tried but I'm getting a syntax error on linevar lowestItems = Object.entries(arr).sort(([ ,v1], [ ,v2]) => v1 - v2)[0]
– Mernt
Mar 25 at 8:06
If you look here developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… you'll see that Internet Explorer does not supportObject.entries
– ChatterOne
Mar 25 at 8:08
2
@Mernt IE doesn't support the fat arrow notation. You need to rewrite([ ,v1], [ ,v2]) => v1 - v2
in old style Javascript
– JollyJoker
Mar 25 at 8:12
|
show 5 more comments
1
Hello, Are there an way to make this script compatible with Internet Explorer too?
– Mernt
Mar 25 at 7:41
@Mernt developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– JollyJoker
Mar 25 at 7:59
I tried but I'm getting a syntax error on linevar lowestItems = Object.entries(arr).sort(([ ,v1], [ ,v2]) => v1 - v2)[0]
– Mernt
Mar 25 at 8:06
If you look here developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… you'll see that Internet Explorer does not supportObject.entries
– ChatterOne
Mar 25 at 8:08
2
@Mernt IE doesn't support the fat arrow notation. You need to rewrite([ ,v1], [ ,v2]) => v1 - v2
in old style Javascript
– JollyJoker
Mar 25 at 8:12
1
1
Hello, Are there an way to make this script compatible with Internet Explorer too?
– Mernt
Mar 25 at 7:41
Hello, Are there an way to make this script compatible with Internet Explorer too?
– Mernt
Mar 25 at 7:41
@Mernt developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– JollyJoker
Mar 25 at 7:59
@Mernt developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– JollyJoker
Mar 25 at 7:59
I tried but I'm getting a syntax error on line
var lowestItems = Object.entries(arr).sort(([ ,v1], [ ,v2]) => v1 - v2)[0]
– Mernt
Mar 25 at 8:06
I tried but I'm getting a syntax error on line
var lowestItems = Object.entries(arr).sort(([ ,v1], [ ,v2]) => v1 - v2)[0]
– Mernt
Mar 25 at 8:06
If you look here developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… you'll see that Internet Explorer does not support
Object.entries
– ChatterOne
Mar 25 at 8:08
If you look here developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… you'll see that Internet Explorer does not support
Object.entries
– ChatterOne
Mar 25 at 8:08
2
2
@Mernt IE doesn't support the fat arrow notation. You need to rewrite
([ ,v1], [ ,v2]) => v1 - v2
in old style Javascript– JollyJoker
Mar 25 at 8:12
@Mernt IE doesn't support the fat arrow notation. You need to rewrite
([ ,v1], [ ,v2]) => v1 - v2
in old style Javascript– JollyJoker
Mar 25 at 8:12
|
show 5 more comments
There are several ways to arrive at what you want. Please see the following methods:
const obj = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49
const values = Object.values(obj)
const lowest = Math.min.apply(null, values)
// Using Object.keys(), Object.values() and findIndex()
const keys = Object.keys(obj)
const indexOfLowest = values.findIndex(function (x) return x === lowest )
console.log(`The lowest value is $lowest from $keys[indexOfLowest]`)
// OR Using Object.keys() and filter()
const key = Object.keys(obj).filter(function (x) return obj[x] === lowest )[0]
console.log(`The lowest value is $lowest from $key`)
// OR Using Object.entries() and sort()
const [[lowestKey, lowestVal]] = Object.entries(obj).sort(function ([,valA], [,valB]) return valA - valB );
console.log(`The lowest value is $lowestVal from $lowestKey`)
It should work as is in IE > = 11
– John Kennedy
Mar 25 at 8:02
Syntax error on linevar key = Object.keys(arr).filter(x => arr[x] === lowest)[0]
– Mernt
Mar 25 at 8:08
1
I'll suggest using a polyfill if it doesn't work in IE. See here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… I should also inform you that on my IE on Windows 10 it works without needing a polyfill.
– John Kennedy
Mar 25 at 8:10
1
@JohnKennedy I'd be surprised by that, IE doesn't support arrow functions
– Nick A
Mar 25 at 14:47
add a comment |
There are several ways to arrive at what you want. Please see the following methods:
const obj = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49
const values = Object.values(obj)
const lowest = Math.min.apply(null, values)
// Using Object.keys(), Object.values() and findIndex()
const keys = Object.keys(obj)
const indexOfLowest = values.findIndex(function (x) return x === lowest )
console.log(`The lowest value is $lowest from $keys[indexOfLowest]`)
// OR Using Object.keys() and filter()
const key = Object.keys(obj).filter(function (x) return obj[x] === lowest )[0]
console.log(`The lowest value is $lowest from $key`)
// OR Using Object.entries() and sort()
const [[lowestKey, lowestVal]] = Object.entries(obj).sort(function ([,valA], [,valB]) return valA - valB );
console.log(`The lowest value is $lowestVal from $lowestKey`)
It should work as is in IE > = 11
– John Kennedy
Mar 25 at 8:02
Syntax error on linevar key = Object.keys(arr).filter(x => arr[x] === lowest)[0]
– Mernt
Mar 25 at 8:08
1
I'll suggest using a polyfill if it doesn't work in IE. See here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… I should also inform you that on my IE on Windows 10 it works without needing a polyfill.
– John Kennedy
Mar 25 at 8:10
1
@JohnKennedy I'd be surprised by that, IE doesn't support arrow functions
– Nick A
Mar 25 at 14:47
add a comment |
There are several ways to arrive at what you want. Please see the following methods:
const obj = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49
const values = Object.values(obj)
const lowest = Math.min.apply(null, values)
// Using Object.keys(), Object.values() and findIndex()
const keys = Object.keys(obj)
const indexOfLowest = values.findIndex(function (x) return x === lowest )
console.log(`The lowest value is $lowest from $keys[indexOfLowest]`)
// OR Using Object.keys() and filter()
const key = Object.keys(obj).filter(function (x) return obj[x] === lowest )[0]
console.log(`The lowest value is $lowest from $key`)
// OR Using Object.entries() and sort()
const [[lowestKey, lowestVal]] = Object.entries(obj).sort(function ([,valA], [,valB]) return valA - valB );
console.log(`The lowest value is $lowestVal from $lowestKey`)
There are several ways to arrive at what you want. Please see the following methods:
const obj = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49
const values = Object.values(obj)
const lowest = Math.min.apply(null, values)
// Using Object.keys(), Object.values() and findIndex()
const keys = Object.keys(obj)
const indexOfLowest = values.findIndex(function (x) return x === lowest )
console.log(`The lowest value is $lowest from $keys[indexOfLowest]`)
// OR Using Object.keys() and filter()
const key = Object.keys(obj).filter(function (x) return obj[x] === lowest )[0]
console.log(`The lowest value is $lowest from $key`)
// OR Using Object.entries() and sort()
const [[lowestKey, lowestVal]] = Object.entries(obj).sort(function ([,valA], [,valB]) return valA - valB );
console.log(`The lowest value is $lowestVal from $lowestKey`)
const obj = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49
const values = Object.values(obj)
const lowest = Math.min.apply(null, values)
// Using Object.keys(), Object.values() and findIndex()
const keys = Object.keys(obj)
const indexOfLowest = values.findIndex(function (x) return x === lowest )
console.log(`The lowest value is $lowest from $keys[indexOfLowest]`)
// OR Using Object.keys() and filter()
const key = Object.keys(obj).filter(function (x) return obj[x] === lowest )[0]
console.log(`The lowest value is $lowest from $key`)
// OR Using Object.entries() and sort()
const [[lowestKey, lowestVal]] = Object.entries(obj).sort(function ([,valA], [,valB]) return valA - valB );
console.log(`The lowest value is $lowestVal from $lowestKey`)
const obj = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49
const values = Object.values(obj)
const lowest = Math.min.apply(null, values)
// Using Object.keys(), Object.values() and findIndex()
const keys = Object.keys(obj)
const indexOfLowest = values.findIndex(function (x) return x === lowest )
console.log(`The lowest value is $lowest from $keys[indexOfLowest]`)
// OR Using Object.keys() and filter()
const key = Object.keys(obj).filter(function (x) return obj[x] === lowest )[0]
console.log(`The lowest value is $lowest from $key`)
// OR Using Object.entries() and sort()
const [[lowestKey, lowestVal]] = Object.entries(obj).sort(function ([,valA], [,valB]) return valA - valB );
console.log(`The lowest value is $lowestVal from $lowestKey`)
edited Mar 25 at 21:42
answered Mar 25 at 6:53
John KennedyJohn Kennedy
3,07321329
3,07321329
It should work as is in IE > = 11
– John Kennedy
Mar 25 at 8:02
Syntax error on linevar key = Object.keys(arr).filter(x => arr[x] === lowest)[0]
– Mernt
Mar 25 at 8:08
1
I'll suggest using a polyfill if it doesn't work in IE. See here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… I should also inform you that on my IE on Windows 10 it works without needing a polyfill.
– John Kennedy
Mar 25 at 8:10
1
@JohnKennedy I'd be surprised by that, IE doesn't support arrow functions
– Nick A
Mar 25 at 14:47
add a comment |
It should work as is in IE > = 11
– John Kennedy
Mar 25 at 8:02
Syntax error on linevar key = Object.keys(arr).filter(x => arr[x] === lowest)[0]
– Mernt
Mar 25 at 8:08
1
I'll suggest using a polyfill if it doesn't work in IE. See here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… I should also inform you that on my IE on Windows 10 it works without needing a polyfill.
– John Kennedy
Mar 25 at 8:10
1
@JohnKennedy I'd be surprised by that, IE doesn't support arrow functions
– Nick A
Mar 25 at 14:47
It should work as is in IE > = 11
– John Kennedy
Mar 25 at 8:02
It should work as is in IE > = 11
– John Kennedy
Mar 25 at 8:02
Syntax error on line
var key = Object.keys(arr).filter(x => arr[x] === lowest)[0]
– Mernt
Mar 25 at 8:08
Syntax error on line
var key = Object.keys(arr).filter(x => arr[x] === lowest)[0]
– Mernt
Mar 25 at 8:08
1
1
I'll suggest using a polyfill if it doesn't work in IE. See here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… I should also inform you that on my IE on Windows 10 it works without needing a polyfill.
– John Kennedy
Mar 25 at 8:10
I'll suggest using a polyfill if it doesn't work in IE. See here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… I should also inform you that on my IE on Windows 10 it works without needing a polyfill.
– John Kennedy
Mar 25 at 8:10
1
1
@JohnKennedy I'd be surprised by that, IE doesn't support arrow functions
– Nick A
Mar 25 at 14:47
@JohnKennedy I'd be surprised by that, IE doesn't support arrow functions
– Nick A
Mar 25 at 14:47
add a comment |
There are many ways to approach your problem, but here is one simple alternative way to do it. Basically, it is a brute force way of doing things, whereby you iterate through every property to check for the value.
const obj = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49;
const getSmallest = function (object)
let smallestValue = Number.MAX_VALUE;
let selectedKey = '';
for (let key in object)
if (object[key] < smallestValue)
smallestValue = object[key];
selectedKey = key;
;
console.log(selectedKey, smallestValue)
return `The lowest value is $smallestValue from $selectedKey`;
;
getSmallest(obj);
add a comment |
There are many ways to approach your problem, but here is one simple alternative way to do it. Basically, it is a brute force way of doing things, whereby you iterate through every property to check for the value.
const obj = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49;
const getSmallest = function (object)
let smallestValue = Number.MAX_VALUE;
let selectedKey = '';
for (let key in object)
if (object[key] < smallestValue)
smallestValue = object[key];
selectedKey = key;
;
console.log(selectedKey, smallestValue)
return `The lowest value is $smallestValue from $selectedKey`;
;
getSmallest(obj);
add a comment |
There are many ways to approach your problem, but here is one simple alternative way to do it. Basically, it is a brute force way of doing things, whereby you iterate through every property to check for the value.
const obj = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49;
const getSmallest = function (object)
let smallestValue = Number.MAX_VALUE;
let selectedKey = '';
for (let key in object)
if (object[key] < smallestValue)
smallestValue = object[key];
selectedKey = key;
;
console.log(selectedKey, smallestValue)
return `The lowest value is $smallestValue from $selectedKey`;
;
getSmallest(obj);
There are many ways to approach your problem, but here is one simple alternative way to do it. Basically, it is a brute force way of doing things, whereby you iterate through every property to check for the value.
const obj = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49;
const getSmallest = function (object)
let smallestValue = Number.MAX_VALUE;
let selectedKey = '';
for (let key in object)
if (object[key] < smallestValue)
smallestValue = object[key];
selectedKey = key;
;
console.log(selectedKey, smallestValue)
return `The lowest value is $smallestValue from $selectedKey`;
;
getSmallest(obj);
edited Mar 25 at 8:12
answered Mar 25 at 6:55
user10997785
add a comment |
add a comment |
To get only the minimum number you can use Math.min(...Object.entries(arr).map(o => o[1]))
:
const arr = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49;
const lowestValue = Math.min(...Object.entries(arr).map(o => o[1]));
console.log(lowestValue);
And to get the object with the minimum value you can use Object.entries(arr).reduce((a, [k, v]) => a[Object.keys(a)[0]] < v ? a : [k]: v, )
:
const arr = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49;
const lowestObj = Object.entries(arr).reduce((a, [k, v]) => a[Object.keys(a)[0]] < v ? a : [k]: v, );
console.log(lowestObj);
add a comment |
To get only the minimum number you can use Math.min(...Object.entries(arr).map(o => o[1]))
:
const arr = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49;
const lowestValue = Math.min(...Object.entries(arr).map(o => o[1]));
console.log(lowestValue);
And to get the object with the minimum value you can use Object.entries(arr).reduce((a, [k, v]) => a[Object.keys(a)[0]] < v ? a : [k]: v, )
:
const arr = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49;
const lowestObj = Object.entries(arr).reduce((a, [k, v]) => a[Object.keys(a)[0]] < v ? a : [k]: v, );
console.log(lowestObj);
add a comment |
To get only the minimum number you can use Math.min(...Object.entries(arr).map(o => o[1]))
:
const arr = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49;
const lowestValue = Math.min(...Object.entries(arr).map(o => o[1]));
console.log(lowestValue);
And to get the object with the minimum value you can use Object.entries(arr).reduce((a, [k, v]) => a[Object.keys(a)[0]] < v ? a : [k]: v, )
:
const arr = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49;
const lowestObj = Object.entries(arr).reduce((a, [k, v]) => a[Object.keys(a)[0]] < v ? a : [k]: v, );
console.log(lowestObj);
To get only the minimum number you can use Math.min(...Object.entries(arr).map(o => o[1]))
:
const arr = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49;
const lowestValue = Math.min(...Object.entries(arr).map(o => o[1]));
console.log(lowestValue);
And to get the object with the minimum value you can use Object.entries(arr).reduce((a, [k, v]) => a[Object.keys(a)[0]] < v ? a : [k]: v, )
:
const arr = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49;
const lowestObj = Object.entries(arr).reduce((a, [k, v]) => a[Object.keys(a)[0]] < v ? a : [k]: v, );
console.log(lowestObj);
const arr = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49;
const lowestValue = Math.min(...Object.entries(arr).map(o => o[1]));
console.log(lowestValue);
const arr = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49;
const lowestValue = Math.min(...Object.entries(arr).map(o => o[1]));
console.log(lowestValue);
const arr = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49;
const lowestObj = Object.entries(arr).reduce((a, [k, v]) => a[Object.keys(a)[0]] < v ? a : [k]: v, );
console.log(lowestObj);
const arr = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49;
const lowestObj = Object.entries(arr).reduce((a, [k, v]) => a[Object.keys(a)[0]] < v ? a : [k]: v, );
console.log(lowestObj);
edited Mar 25 at 8:01
answered Mar 25 at 7:03
Yosvel QuinteroYosvel Quintero
12.1k42531
12.1k42531
add a comment |
add a comment |
Here we first assume that the min, key and value as first property and the value respectively. Then we iterate through all the elements and compare with minValue to find any value less than that in the object. If it is, then we update both minValue and minKey. Anyway, the arr type is an Object. it is not an array in javascript.
var arr = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49;
var minkey = Object.keys(arr)[0];
var minValue = arr[Object.keys(arr)[0]];
for (var key in arr)
if (arr.hasOwnProperty(key))
if(arr[key] < minValue)
minValue = arr[key];
minKey = key;
add a comment |
Here we first assume that the min, key and value as first property and the value respectively. Then we iterate through all the elements and compare with minValue to find any value less than that in the object. If it is, then we update both minValue and minKey. Anyway, the arr type is an Object. it is not an array in javascript.
var arr = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49;
var minkey = Object.keys(arr)[0];
var minValue = arr[Object.keys(arr)[0]];
for (var key in arr)
if (arr.hasOwnProperty(key))
if(arr[key] < minValue)
minValue = arr[key];
minKey = key;
add a comment |
Here we first assume that the min, key and value as first property and the value respectively. Then we iterate through all the elements and compare with minValue to find any value less than that in the object. If it is, then we update both minValue and minKey. Anyway, the arr type is an Object. it is not an array in javascript.
var arr = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49;
var minkey = Object.keys(arr)[0];
var minValue = arr[Object.keys(arr)[0]];
for (var key in arr)
if (arr.hasOwnProperty(key))
if(arr[key] < minValue)
minValue = arr[key];
minKey = key;
Here we first assume that the min, key and value as first property and the value respectively. Then we iterate through all the elements and compare with minValue to find any value less than that in the object. If it is, then we update both minValue and minKey. Anyway, the arr type is an Object. it is not an array in javascript.
var arr = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49;
var minkey = Object.keys(arr)[0];
var minValue = arr[Object.keys(arr)[0]];
for (var key in arr)
if (arr.hasOwnProperty(key))
if(arr[key] < minValue)
minValue = arr[key];
minKey = key;
answered Mar 25 at 7:02
HarshanaHarshana
346212
346212
add a comment |
add a comment |
You can try this:
obj = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2; // This is an object, That;s ehy you getting infinite
var arr = Object.keys( obj ).map(function ( key ) return obj[key]; );
alert(Math.min.apply( null, arr ));
Fiddle: https://jsfiddle.net/ozvubmdr/
add a comment |
You can try this:
obj = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2; // This is an object, That;s ehy you getting infinite
var arr = Object.keys( obj ).map(function ( key ) return obj[key]; );
alert(Math.min.apply( null, arr ));
Fiddle: https://jsfiddle.net/ozvubmdr/
add a comment |
You can try this:
obj = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2; // This is an object, That;s ehy you getting infinite
var arr = Object.keys( obj ).map(function ( key ) return obj[key]; );
alert(Math.min.apply( null, arr ));
Fiddle: https://jsfiddle.net/ozvubmdr/
You can try this:
obj = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2; // This is an object, That;s ehy you getting infinite
var arr = Object.keys( obj ).map(function ( key ) return obj[key]; );
alert(Math.min.apply( null, arr ));
Fiddle: https://jsfiddle.net/ozvubmdr/
answered Mar 25 at 6:51
Ajay MakwanaAjay Makwana
1,537926
1,537926
add a comment |
add a comment |
Easy, though not optimal, as it traverses the array twice: find the smallest value, then find the key belonging to it.
arr = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2
min = Math.min(...Object.values(arr));
minkey = Object.keys(arr).find(key => arr[key] = min);
console.log(`Lowest is $min at $minkey`);
add a comment |
Easy, though not optimal, as it traverses the array twice: find the smallest value, then find the key belonging to it.
arr = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2
min = Math.min(...Object.values(arr));
minkey = Object.keys(arr).find(key => arr[key] = min);
console.log(`Lowest is $min at $minkey`);
add a comment |
Easy, though not optimal, as it traverses the array twice: find the smallest value, then find the key belonging to it.
arr = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2
min = Math.min(...Object.values(arr));
minkey = Object.keys(arr).find(key => arr[key] = min);
console.log(`Lowest is $min at $minkey`);
Easy, though not optimal, as it traverses the array twice: find the smallest value, then find the key belonging to it.
arr = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2
min = Math.min(...Object.values(arr));
minkey = Object.keys(arr).find(key => arr[key] = min);
console.log(`Lowest is $min at $minkey`);
arr = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2
min = Math.min(...Object.values(arr));
minkey = Object.keys(arr).find(key => arr[key] = min);
console.log(`Lowest is $min at $minkey`);
arr = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2
min = Math.min(...Object.values(arr));
minkey = Object.keys(arr).find(key => arr[key] = min);
console.log(`Lowest is $min at $minkey`);
answered Mar 25 at 6:54
AmadanAmadan
134k13150201
134k13150201
add a comment |
add a comment |
You may try this:
const arr = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2
const minVal = Math.min(...Object.values(arr))
const fromKey = Object.keys(arr).find(key => arr[key] === minVal)
console.log(`The lowest value is $minVal from $fromKey`)
add a comment |
You may try this:
const arr = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2
const minVal = Math.min(...Object.values(arr))
const fromKey = Object.keys(arr).find(key => arr[key] === minVal)
console.log(`The lowest value is $minVal from $fromKey`)
add a comment |
You may try this:
const arr = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2
const minVal = Math.min(...Object.values(arr))
const fromKey = Object.keys(arr).find(key => arr[key] === minVal)
console.log(`The lowest value is $minVal from $fromKey`)
You may try this:
const arr = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2
const minVal = Math.min(...Object.values(arr))
const fromKey = Object.keys(arr).find(key => arr[key] === minVal)
console.log(`The lowest value is $minVal from $fromKey`)
const arr = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2
const minVal = Math.min(...Object.values(arr))
const fromKey = Object.keys(arr).find(key => arr[key] === minVal)
console.log(`The lowest value is $minVal from $fromKey`)
const arr = lst1: 300, lst2: 381, lst3: 4, lst4: 4, lst5: 49, lst9: 2
const minVal = Math.min(...Object.values(arr))
const fromKey = Object.keys(arr).find(key => arr[key] === minVal)
console.log(`The lowest value is $minVal from $fromKey`)
answered Mar 25 at 6:59
Pak Wah WongPak Wah Wong
38317
38317
add a comment |
add a comment |
You can use Object.entries
and use Array.reduce
to reduce the resulting [key-value] pair to an object which will have lowest entry:
const data = lst1: 300, lst2: -381, lst3: 4, lst4: 4, lst5: 49, lst6: -49, lst7: 0, lst7: 78, lst7: -90;
const obj = Object.entries(data).reduce((acc, [key, value]) =>
acc["lowest"] = acc["lowest"] ? Object.values(acc["lowest"])[0] < value ? acc["lowest"] : [key] : value : [key] : value ;
return acc;
, );
console.log(`Lowest value is found in $JSON.stringify(obj.lowest)`);
add a comment |
You can use Object.entries
and use Array.reduce
to reduce the resulting [key-value] pair to an object which will have lowest entry:
const data = lst1: 300, lst2: -381, lst3: 4, lst4: 4, lst5: 49, lst6: -49, lst7: 0, lst7: 78, lst7: -90;
const obj = Object.entries(data).reduce((acc, [key, value]) =>
acc["lowest"] = acc["lowest"] ? Object.values(acc["lowest"])[0] < value ? acc["lowest"] : [key] : value : [key] : value ;
return acc;
, );
console.log(`Lowest value is found in $JSON.stringify(obj.lowest)`);
add a comment |
You can use Object.entries
and use Array.reduce
to reduce the resulting [key-value] pair to an object which will have lowest entry:
const data = lst1: 300, lst2: -381, lst3: 4, lst4: 4, lst5: 49, lst6: -49, lst7: 0, lst7: 78, lst7: -90;
const obj = Object.entries(data).reduce((acc, [key, value]) =>
acc["lowest"] = acc["lowest"] ? Object.values(acc["lowest"])[0] < value ? acc["lowest"] : [key] : value : [key] : value ;
return acc;
, );
console.log(`Lowest value is found in $JSON.stringify(obj.lowest)`);
You can use Object.entries
and use Array.reduce
to reduce the resulting [key-value] pair to an object which will have lowest entry:
const data = lst1: 300, lst2: -381, lst3: 4, lst4: 4, lst5: 49, lst6: -49, lst7: 0, lst7: 78, lst7: -90;
const obj = Object.entries(data).reduce((acc, [key, value]) =>
acc["lowest"] = acc["lowest"] ? Object.values(acc["lowest"])[0] < value ? acc["lowest"] : [key] : value : [key] : value ;
return acc;
, );
console.log(`Lowest value is found in $JSON.stringify(obj.lowest)`);
const data = lst1: 300, lst2: -381, lst3: 4, lst4: 4, lst5: 49, lst6: -49, lst7: 0, lst7: 78, lst7: -90;
const obj = Object.entries(data).reduce((acc, [key, value]) =>
acc["lowest"] = acc["lowest"] ? Object.values(acc["lowest"])[0] < value ? acc["lowest"] : [key] : value : [key] : value ;
return acc;
, );
console.log(`Lowest value is found in $JSON.stringify(obj.lowest)`);
const data = lst1: 300, lst2: -381, lst3: 4, lst4: 4, lst5: 49, lst6: -49, lst7: 0, lst7: 78, lst7: -90;
const obj = Object.entries(data).reduce((acc, [key, value]) =>
acc["lowest"] = acc["lowest"] ? Object.values(acc["lowest"])[0] < value ? acc["lowest"] : [key] : value : [key] : value ;
return acc;
, );
console.log(`Lowest value is found in $JSON.stringify(obj.lowest)`);
edited Mar 25 at 7:30
answered Mar 25 at 7:02
Amardeep BhowmickAmardeep Bhowmick
5,78121128
5,78121128
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55332453%2fgetting-the-lowest-value-with-key-in-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What is expected output?
– Maheer Ali
Mar 25 at 6:45
3
Do you want to get, for example,
lst3
ifobj.lst3
is the lowest value?– Jack Bashford
Mar 25 at 6:47
3
Also, what is
lst9
? Is itlst9: 2
?– Jack Bashford
Mar 25 at 6:48
may be a duplicte of stackoverflow.com/questions/38008307/…
– Syed Mehtab Hassan
Mar 25 at 6:49
10
arr
is not an array but anobject
– Isaac
Mar 25 at 6:53