JavaScript Get object keys with the highest value in Javascript object keys with spaces javascript,object keys with condition,object.

Imagine an object like this:

var values = {
    "2": 1,
    "53": 2,
    "56": 4,
    "57": 9,
    "61": 2,
    "62": 16,
    "63": 2,
    "398": 24,
    ...
}

My goal is, to find the 10 object keys, which have the highest value. In this case: 398, then 62 and so on (= [398, 62, ...]). I know how I can put this into an array, don't know how to receive the property key though.

Important: I can't change the format because it's a server response.

I tried with a for (key in values) {} loop but have no idea how to move on. This similar question and it's answer couldn't really help me either.

Answer:1

As commented before:

  • Create an array of keys: Object.keys(object)
  • Sort this array based on value: sort((a,b)=> object[b] - object[a])
  • Get necessary values: keys.slice(0,n)

var value = {2:1,53:2,56:4,57:9,61:2,62:16,63:2,398:24};

function getKeysWithHighestValue(o, n){
  var keys = Object.keys(o);
  keys.sort(function(a,b){
    return o[b] - o[a];
  })
  console.log(keys);
  return keys.slice(0,n);
}

console.log(getKeysWithHighestValue(value, 4))
Answer:2

You could sort by the values property and slice the result.

var values = { 2: 1, 53: 2, 56: 4, 57: 9, 61: 2, 62: 16, 63: 2, 398: 24 },
    result = Object
        .keys(values)
        .sort(function (a, b) { return values[b] - values[a]; })
        .slice(0, 10)
        .map(Number);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Answer:3

You can use Object.keys to get key values

var value = {
"2": 1,
"53": 2,
"56": 4,
"57": 9,
"61": 2,
"62": 16,
"63": 2,
"398": 24,
}
var parsedKeys = Object.keys(value).map(x => parseInt(x));
var sortedKeys = parsedKeys.sort(function (a, b) {  return b - a;  });

Now you can get highest top 10 keys by sortedKeys.slice(0, 10);

Answer:4

I work entering data online to a form that I would like to change the tabbing order but have had no luck using Greasemonkey and Scriptish. I am able to remove tabindexes of -1 to make fields ...

I work entering data online to a form that I would like to change the tabbing order but have had no luck using Greasemonkey and Scriptish. I am able to remove tabindexes of -1 to make fields ...

  1. setting tabindex using css
  2. setting tabindex in javascript
  3. setting tabindex in html
  4. setting tabindex in css
  5. setting tabindex in jquery
  6. setting tabindex jquery

I am using Express.js server. With cookie-parser I have opened this endpoint app.get("/s", (req,res) => { res.cookie("bsaSession", req.session.id) res.send("set cookie ok") }) When I ...

I am using Express.js server. With cookie-parser I have opened this endpoint app.get("/s", (req,res) => { res.cookie("bsaSession", req.session.id) res.send("set cookie ok") }) When I ...

I'm trying to draw a map with d3.js. I already did it a first time and it worked, but now I try to split my code to make it easier to read. When i split it i get NaN instead of position in my path. ...

I'm trying to draw a map with d3.js. I already did it a first time and it worked, but now I try to split my code to make it easier to read. When i split it i get NaN instead of position in my path. ...

  1. d3js path data
  2. d3js path d
  3. d3js path drag
  4. d3js draw path

I'm having problem with using Kendo UI Grid whenever I click on add new button it always produce this script error. This is the code that I'm using: dataSource = new kendo.data.DataSource({ ...

I'm having problem with using Kendo UI Grid whenever I click on add new button it always produce this script error. This is the code that I'm using: dataSource = new kendo.data.DataSource({ ...

  1. uncaught typeerror e is not a function
  2. uncaught typeerror e b is not a function
  3. uncaught typeerror e is not a function bootstrap
  4. uncaught typeerror e h is not a function
  5. uncaught typeerror e o is not a function
  6. uncaught typeerror e.indexof is not a function
  7. uncaught typeerror e.indexof is not a function at w.fn.init.w.fn.load
  8. uncaught typeerror e.preventdefault is not a function
  9. uncaught typeerror e.indexof is not a function at k.fn.init.k.fn.load
  10. uncaught typeerror e.replace is not a function
  11. uncaught typeerror e is not a constructor
  12. uncaught typeerror e.indexof is not a function jquery
  13. uncaught typeerror e.push is not a function
  14. uncaught typeerror e.size is not a function
  15. uncaught typeerror e.indexof is not a function jquery 3.3.1
  16. uncaught typeerror e.stoppropagation is not a function
  17. uncaught typeerror e.indexof is not a function wordpress
  18. uncaught typeerror e.fancybox.getinstance is not a function
  19. uncaught typeerror e.split is not a function
  20. uncaught typeerror e.target.classname.indexof is not a function