How to find the smallest value in a JSON object with JavaScript

1 Answer

0 votes
const findSmallestValue = obj => {
    const smallest = Object.keys(obj).reduce((accumulator, currentValue) => {
        return Math.min(accumulator, obj[currentValue]);
    }, Infinity);
    
    return smallest;
}

const obj = {
   "a": 4,
   "b": 8,
   "c": 5,
   "d": 3,
   "e": 5,
   "f": 9,
   "g": 7,
};

console.log(findSmallestValue(obj));




/*
run:

3

*/

 



answered Mar 22, 2024 by avibootz

Related questions

3 answers 297 views
2 answers 220 views
1 answer 176 views
1 answer 201 views
1 answer 153 views
2 answers 154 views
...