I'm working on converting an array utilities
file from js to typescript on an open source project. It's been pretty easy to convert most functions, but I'm not understanding what this one is doing.
/**
* sort array by numeric by numeric property values
* of object entries. null entries are treated as 0.
* array entries must be objects.
* @param {object[]} arr
* @param {string} prop - numeric property to sort on.
*/
export const propSort = (arr,prop) => {
arr.sort( (a,b)=>{
return ( a[prop] || 0 ) - ( b[prop] || 0 );
});
}
I have a description for what the function is supposed to be doing, as is listed above the function. But I don't understand the syntax. Apparently prop is supposed to be a string. But if that's the case, what exactly are a, b? How do you get the value out of an array using a string? And are there any pitfalls in converting this function to typescript?