vue js 比较版本号大小
function compareVersion(v1, v2) {
if (v1 == v2) {
return 0;
}
const vs1 = v1.split(".").map(a => parseInt(a));
const vs2 = v2.split(".").map(a => parseInt(a));
const length = Math.min(vs1.length, vs2.length);
for (let i = 0; i < length; i++) {
if (vs1[i] > vs2[i]) {
return 1;
} else if (vs1[i] < vs2[i]) {
return -1;
}
}
if (length == vs1.length) {
return -1;
} else {
return 1;
}
}
// 版本号判断测试
// if (
// compareVersion("3.1.5", "3.1.4") == -1 ||
// compareVersion("3.1.5", "3.1.4") == 0
// ) {
// console.log("大于等于");
// } else {
// console.log("小于");
// }
// console.log("版本号", compareVersion("3.1.5", "3.1.4"));
//情况一:
compareVersion('3.1.5','3.1.4'); //1
//当 v1 大于 v2 时返回 1
//情况二:
compareVersion('3.1.5','3.1.5'); //0
//当 v1 等于 v2 时返回 0
//情况二:
compareVersion('3.1.5','3.1.6''); //-1
//当 v1 小于 v2 时返回 -1