JavaScript How to find highest key in JSON?

Object.keys() is a good place to start for jobs like this. How about something like this?

var data = {
  "scsi0": "vm-101-disk-1.qcow2,size=32G",
  "scsi1": "vm-101-disk-2.qcow2,size=32G",
  "scsi2": "vm-101-disk-3.qcow2,size=32G"
};

// get keys of data
var keys = Object.keys(data);

// get array of number values
var numbers = keys.map(function(key) {
  // strip text out of keys, parse as integers
  return parseInt(key.replace(/\D/g, '') || 0);
})

// get the largest number in the array
var highest = Math.max.apply(null, numbers);

// build the data key with this number
var key = "scsi" + highest;

// get the data pertaining to the key
var final = data[key];

// log the result
console.log(final);
Answer:1

You could use a collator to get a compare function that takes such embedded numbers into account:

const data = {
    "scsi0": "vm-101-disk-1.qcow2,size=32G",
    "scsi11": "vm-101-disk-2.qcow2,size=32G",
    "scsi2": "vm-101-disk-3.qcow2,size=32G"
};

const cmp = (new Intl.Collator(undefined, {numeric: true})).compare;
const max = Object.keys(data).reduce((a, b) => cmp(a, b) > 0 ? a : b);

console.log(max)
Answer:2

My submit button is finally working. However, it is not counting all of the votes in the results. How do I get my submit button to sort all of the votes? HTML: <!DOCTYPE html> <html&...

My submit button is finally working. However, it is not counting all of the votes in the results. How do I get my submit button to sort all of the votes? HTML: <!DOCTYPE html> <html&...

  1. sort keys array php
  2. sort keys array javascript
  3. sort array keys
  4. sort array keys alphabetically php
  5. sort array keys descending php
  6. sort array keys js
  7. sort array keys descending order php
  8. sort array keys by value

I have two components: <div> <advertiser-add-component></advertiser-add-component> <advertisers-component></advertisers-component> </div> These components ...

I have two components: <div> <advertiser-add-component></advertiser-add-component> <advertisers-component></advertisers-component> </div> These components ...

I have a question whether this is possible or not. For example, there is one code like below. But that code has an error for sure. And I want to make if is true when one data is at some array. Could ...

I have a question whether this is possible or not. For example, there is one code like below. But that code has an error for sure. And I want to make if is true when one data is at some array. Could ...

Firebase has a function 'createUserWithEmailAndPassword' that can be called from the frontend to create a user. What prevents someone from manually invoking the function from inside your javascript ...

Firebase has a function 'createUserWithEmailAndPassword' that can be called from the frontend to create a user. What prevents someone from manually invoking the function from inside your javascript ...

  1. how to prevent drug abuse
  2. what prevents animal abuse