Вы можете поместить эти разбиения в одну строку и использовать назначение деструктуры, чтобы получить width
и height
:
const [width, height] = myURL.split("/")[5].split("x");
Или использовать RegEx:
const [width, height] = url.match(/\d+x\d+/)[0].split('x');
const url = `//a.storyblok.com/f/53830/6015x3900/7cea8305a6/mohit-singh-312892-unsplash.jpg`;
function getURL(myURL) {
if (url != null) {
const [width, height] = myURL.split("/")[5].split("x");
const calc = (width / height) * 100;
return calc;
} else {
return;
}
}
const result = getURL(url);
console.log(result);
/***********************/
const getUrl2 = url => {
const [width, height] = url.match(/\d+x\d+/)[0].split('x')
return (width / height) * 100;
}
const result2 = getUrl2(url);
console.log(result2)