1

I have this nested objects


var obj = {
    first:{
        num:10
    },
    second: {
        num:5
    },
    third: {
        num: 15
    }
}

console.log(obj);

so there could be a lot of nested objects inside and i need to get the object with highest num value inside which in this case is the third object.

So i should get as output

third: {
        num: 15
 }

I got stuck at the iteration

var arr = Object.keys( obj ).map(function ( key, index ) { 
    console.log(obj[key]);
 });
console.log(arr);
sparrow123
  • 55
  • 6
  • It would be much simpler to have an array of numbers. You're complicating your data structure too much. – Andy Jan 24 '22 at 10:05
  • `third: { num: 15 }` is not an object. `{third: { num: 15 }}` OR `{ num: 15 }` can be an object. I am not sure in what for you are expecting an answer. – AppleCiderGuy Jan 24 '22 at 10:07
  • you should scrolling your array first and copy obj["num"] to new array, then look at this question: https://stackoverflow.com/questions/4020796 – mohsen solhnia Jan 24 '22 at 10:10

4 Answers4

1

as I mentioned in my comment above, what you mentioned as expected output is not really an object. however, you can use following snippet to find the key within ur main object which holds the highest num:

var running = Number.MIN_VALUE;
var ans = "";
Object.keys(object).forEach(key => {
    if(object[key]["num"] > running) {
        ans = key;
    }
})
console.log(JSON.stringify(object[ans])); //outputs { num: 15 }
AppleCiderGuy
  • 1,249
  • 1
  • 9
  • 16
1

If you want the whole object back with the high value, I would use reduce

var obj = {
    first:{
        num:10
    },
    second: {
        num:5
    },
    third: {
        num: 15
    }
}

const result = Object.entries(obj)
                     .reduce( (acc,[k,i]) => Object.values(acc)[0].num<i.num ? {[k]:i} : acc
                                             , {zero:{num:0}})

console.log(result);
Jamiec
  • 133,658
  • 13
  • 134
  • 193
0

No need for Object.keys (though that would be fine for getting an array of the object keys to check) or map, just a simple loop where you remember the object with the highest number you've seen so far:

let found = null;
// Loop through the keys of the object
for (const key in obj) {
    // Get the value for this key
    const entry = obj[key];
    // If this is an own property of the object and either A) We
    // don't have any "found" object yet or the one we do have has
    // a lower `num` than this one, remember this one
    if (Object.hasOwn(obj, key) && (!found || found.num < entry.num)) {
        found = entry;
    }
}

Live Example:

const obj = {
    first:{
        num:10
    },
    second: {
        num:5
    },
    third: {
        num: 15
    }
};

let found = null;
for (const key in obj) {
    const entry = obj[key];
    if (Object.hasOwn(obj, key) && (!found || found.num < entry.num)) {
        found = entry;
    }
}
console.log(found);

If you also need to remember the property name, just do that at the same time you assign to found.


That uses the newish Object.hasOwn function. If you need to support implementations that don't have it, use a curated polyfill or just:

if (!Object.hasOwn) {
    Object.defineProperty(Object, "hasOwn", {
        value: Function.prototype.call.bind(Object.prototype.hasOwnProperty),
        writable: true,
        configurable: true,
    });
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

The problem here is your nested (and bit complicated) structure. I may over engineer this problem a bit, but at least following snippet does what you really want for.

First you need to map through entries and then compare them in a reducer, which would ultimately return the object with highest value. But you need also the original key of a nested object, thus we need to keep it along and finally convert the object back as original.

function convertAsOriginal also gives you freedom to mutate the final result, in case you need some additional keys/values or want to rename them

const obj = {
  first: { num: 5 },
  second: { num: 25 },
  third: { num: 15 },
  fourth: { num: 0 },
  fifth: { num: 10 },
};


const mapThroughEntries = ([key, val]) => ({key, ...val});
const reduceToHighest = (acc, b) => (acc.num > b.num ? acc : b);
const convertAsOriginal = (obj) => ({[obj.key]: {'num': obj.num}});

const highest = convertAsOriginal(
  Object.entries(obj)
    .map(mapThroughEntries)
    .reduce(reduceToHighest)
);

console.log(highest);
Samuli Hakoniemi
  • 18,740
  • 1
  • 61
  • 74