1
var food = {
"fruit":{"brand":{"grgrdgdr":true,"ggyugy":true}, "sold":2},
"vegetable" :{"brand":{"htrhtr":true},"sold": 1},
"snack":{"brand":{"htrhr":true},"sold": 3},
"other":{"brand":{"gdhshd":true},"sold":1},
....
...
...

};

How do I console log the object name that sold the most, in this case should be the word snack.

I find this link, but its structure is not similar to mine. Get object keys with the highest value in Javascript

any help is appreciated thanks in advance

Tosps
  • 73
  • 7

5 Answers5

1

You could sort by the values property and get the first item.

var food = { fruit: { brand: { grgrdgdr: true, ggyugy: true }, sold: 2 }, vegetable: { brand: { htrhtr: true }, sold: 1 }, snack: { brand: { htrhr: true }, sold: 3 }, other: { brand: { gdhshd: true }, sold: 1 } };
    result = Object
        .keys(food)
        .sort((a, b) => food[b].sold - food[a].sold)
        [0];

console.log(result);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

Find the max value first, and then filter the object to get objects having max sold value

var food = {
"fruit":{"brand":{"grgrdgdr":true,"ggyugy":true}, "sold":2},
"vegetable" :{"brand":{"htrhtr":true},"sold": 1},
"snack":{"brand":{"htrhr":true},"sold": 3},
"other":{"brand":{"gdhshd":true},"sold":1}}

var maxSold = Math.max(...Object.values(food).map(o=> o.sold))

var result = Object.keys(food).filter(o => food[o].sold ===maxSold)
console.log(result) //this will given all objects with max sold value
Harish
  • 1,841
  • 1
  • 13
  • 26
0

You can use a variable & iterate the array twice. In the first iteration get the highest sold. Then on second iteration add the products whose sold value is same as the highest sold

var food = {
  "fruit": {
    "brand": {
      "grgrdgdr": true,
      "ggyugy": true
    },
    "sold": 5
  },
  "vegetable": {
    "brand": {
      "htrhtr": true
    },
    "sold": 1
  },
  "snack": {
    "brand": {
      "htrhr": true
    },
    "sold": 3
  },
  "other": {
    "brand": {
      "gdhshd": true
    },
    "sold": 1
  },
  "someOtherfruit": {
    "brand": {
      "grgrdgdr": true,
      "ggyugy": true
    },
    "sold": 5
  }
}

let highestSold = 0;
let newObj = [];

for (let keys in food) {
  if (food[keys].sold > highestSold) {
    highestSold = food[keys].sold
  }
}

for (let keys in food) {
  if (food[keys].sold === highestSold) {
    newObj.push(food[keys])
  }
}

console.log(newObj[0])
brk
  • 48,835
  • 10
  • 56
  • 78
0

Your posted link Get object keys with the highest value in Javascript

was quite close

var food = {
"fruit":{"brand":{"grgrdgdr":true,"ggyugy":true}, "sold":2},
"vegetable" :{"brand":{"htrhtr":true},"sold": 1},
"snack":{"brand":{"htrhr":true},"sold": 3},
"other":{"brand":{"gdhshd":true},"sold":1}
  },
  result = Object
  .keys(food)
  .sort(function(a, b) {
    return food[b].sold - food[a].sold;
  })[0] // or  .slice(0, 1); 

console.log(result,food[result]);
.as-console-wrapper {
  max-height: 100% !important;
  top: 0;
}
mplungjan
  • 169,008
  • 28
  • 173
  • 236
0

var food = {
"fruit":{"brand":{"grgrdgdr":true,"ggyugy":true}, "sold":2},
"vegetable" :{"brand":{"htrhtr":true},"sold": 1},
"snack":{"brand":{"htrhr":true},"sold": 3},
"other":{"brand":{"gdhshd":true},"sold":1},
};


function getMostExpensivesKey(array) {
  let key = Object.keys(array)[0];

  Object.keys(array).forEach(function(k) {
    if(array[k].sold > array[key].sold) {
      key = k;
    }
  });

  return key;
}


console.log(getMostExpensivesKey(food));
mplungjan
  • 169,008
  • 28
  • 173
  • 236
PopHip
  • 700
  • 6
  • 23