An alternative could be a regex pattern, but this is as brittle as any solution that assumes URL structures will never change. Relying on URLs for the id is probably not the most solid approach. Surely the JSON stream holds the actual ids somewhere, at least indirectly in the linked objects?
Explanation of the matching pattern /\/(\d+)\/?$/
used in the example below:
\/
matches a forward slash, escaped with a backslash.(\d+)
gets one + more digits\/?
gets optional /, escaping it.$
matches end of stringMost of this is touched upon in the Regular expressions at MDN. Also see String.prototype.match() for this method.
const url = 'https://jsonplaceholder.typicode.com/todos/101/';
const idInUrlPattern = /\/(\d+)\/?$/;
//? handles a null case, [1] returns the matching group
const getIdFromUrl = url => url.match(idInUrlPattern)?.[1];
console.log(getIdFromUrl(url));