HI sorry am a beginner here would want to ask how to return an object that has a key that describes the version?
myFunction(“15.0.9”)
returns { “major”: 15, “minor”: 0, “patch”: 9 }
myFunctuion(“12.2.4”)
returns { “major”: 12, “minor”: 2, “patch”: 4 }
CodePudding user response:
You can just split()
the string into it's components, map()
all the components to numbers using the unary plus operator, and return an object:
const semver = (s) => {
const [major, minor, patch] = s.split('.').map(v => v);
return { major, minor, patch };
}
console.log(semver("15.0.9"));
CodePudding user response:
Try this:
var GimmeVer = function(vNo){
var arr = vNo.split('.');
var res = {
"major":0,
"minor":0,
"patch":0
};
for (var i = 0; i < arr.length; i ){
if (i == 0)
res.major=arr[i];
if (i == 1)
res.minor = arr[i];
if (i == 2)
res.patch = arr[i];
return res;
}
CodePudding user response:
Try This
function myFunction(data){
const keys = ['major', 'minor', 'patch'];
return data.split('.').reduce((acc, cur, i) => {
if(keys[i]) acc[keys[i]] = Number(cur);
return acc;
}, {});
}