0

I am stuck when looks at this solution whch talks about how to sorting object property by values

const maxSpeed = {
    car: 300, 
    bike: 60, 
    motorbike: 200, 
    airplane: 1000,
    helicopter: 400, 
    rocket: 8 * 60 * 60
};
console.log( Object.entries(maxSpeed))
const sortable = Object.fromEntries(

    Object.entries(maxSpeed).sort(([,a],[,b]) => {
    console.log(a,b)
    return a-b
    })
);

console.log(sortable);

Why using a Trailing commas there, why it makes it returns [1] item in the nested array (e.g. [car,300 ][0])?

By looking at mdn descrption about the trailing comma, I still don't have any ideas:

Trailing commas (sometimes called "final commas") can be useful when adding new elements, parameters, or properties to JavaScript code. If you want to add a new property, you can add a new line without modifying the previously last line if that line already uses a trailing comma. This makes version-control diffs cleaner and editing code might be less troublesome.

Any hints and comments will be appreciated.

Jason
  • 3
  • 5
  • FYI "trailing" means _after_ but the commas in the code here are "preceding", ie _before_. Getting the terms right will help with searches in future – Phil Mar 23 '22 at 22:40
  • `console.log([,,0,,0]);` now with destructuring `[, a]` it is saying I do not care about index 0, but I want index 1 to be stored in variable `a` – epascarello Mar 23 '22 at 22:40
  • @Phil, thank you!! Just curious how do we call the comma in the js code here? When I search online, search comma in js, the top result is just trailing operator and comma operator – Jason Mar 23 '22 at 23:08
  • @epascarello, thanks, this make more sense and clear! – Jason Mar 23 '22 at 23:08
  • Yeah, this would be a tricky one to search for so I don't blame you for not finding answers. The technical term for this would be _"omitting indexes"_ or _"specific index"_ in _"javascript array destructuring"_ – Phil Mar 23 '22 at 23:45
  • @Phil, thanks so much;) That makes more sense – Jason Mar 23 '22 at 23:48

0 Answers0