简体   繁体   English

JavaScript 从虚线版本数组中找到最高版本

[英]JavaScript find highest version from array of dotted versions

I have a array of version numbers looking like this:我有一系列版本号,如下所示:

[
  {
    "name": "v12.3.0.pre",
  },
  {
    "name": "v12.2.5",
  },
  {
    "name": "v12.2.4",
  },
  {
    "name": "v12.2.3",
  },
  {
    "name": "v12.2.1",
  },
  {
    "name": "v12.2.0",
  },
  {
    "name": "v12.2.0.pre",
  },
  {
    "name": "v12.2.0-rc32",
  },
  {
    "name": "v12.2.0-rc31",
  },
  {
    "name": "v12.1.9",
  },
  {
    "name": "v12.1.8",
  },
  {
    "name": "v12.1.6",
  },
  {
    "name": "v12.1.4",
  },
  {
    "name": "v12.1.3",
  },
  {
    "name": "v12.1.2",
  },
  {
    "name": "v12.1.1",
  },
  {
    "name": "v12.1.0",
  },
  {
    "name": "v12.1.0.pre",
  },
  {
    "name": "v12.1.0-rc23",
  },
  {
    "name": "v12.1.0-rc22",
  },
  {
    "name": "v12.0.9",
  },
  {
    "name": "v12.0.8",
  },
  {
    "name": "v12.0.6",
  },
  {
    "name": "v12.0.4",
  },
  {
    "name": "v12.0.3",
  },
  {
    "name": "v12.0.2",
  },
  {
    "name": "v12.0.1",
  },
  {
    "name": "v12.0.0",
  },
  {
    "name": "v11.12.0.pre",
  },
  {
    "name": "v11.11.8",
  }
]

From this array I would like to determine the latest version, which do not end with '.pre' or include 'rc.从这个数组中我想确定最新版本,它不以'.pre'结尾或包含'rc。

I'm iterating through the array with a for-loop, and filtering out the '.pre' and 'rc' with an if statement.我正在使用 for 循环遍历数组,并使用 if 语句过滤掉“.pre”和“rc”。 I then use split/join to remove the first 'v' character.然后我使用 split/join 删除第一个 'v' 字符。 So far so good.到目前为止,一切都很好。 Then I'm left with values like '12.2.5' and '11.12.10'.然后我留下了像'12.2.5'和'11.12.10'这样的值。 I first thought of removing the dots, then use a 'greater than' operator to see find the highest value, but then '11.12.10(111210)' would result greater than '12.2.5(1225)' which would not work out in my case.我首先想到删除点,然后使用“大于”运算符来查看找到最大值,但随后“11.12.10(111210)”会导致大于“12.2.5(1225)”,这是行不通的就我而言。

for(i in arr){
    if(!arr[i].name.endsWith('.pre') && !arr[i].name.includes('rc')){
        var number = number.split('v').join("");
        var number = number.split('.').join("");
    }
}

Any ideas on best way to solve this?有关解决此问题的最佳方法的任何想法? Thanks!谢谢!

You could take String#localeCompare with options for getting a result.您可以将String#localeCompare与获取结果的选项一起使用。

 var data = [{ name: "v12.3.0.pre" }, { name: "v12.2.5" }, { name: "v12.2.4" }, { name: "v12.2.3" }, { name: "v12.2.1" }, { name: "v12.2.0" }, { name: "v12.2.0.pre" }, { name: "v12.2.0-rc32" }, { name: "v12.2.0-rc31" }, { name: "v12.1.9" }, { name: "v12.1.8" }, { name: "v12.1.6" }, { name: "v12.1.4" }, { name: "v12.1.3" }, { name: "v12.1.2" }, { name: "v12.1.1" }, { name: "v12.1.0" }, { name: "v12.1.0.pre" }, { name: "v12.1.0-rc23" }, { name: "v12.1.0-rc22" }, { name: "v12.0.9" }, { name: "v12.0.8" }, { name: "v12.0.6" }, { name: "v12.0.4" }, { name: "v12.0.3" }, { name: "v12.0.2" }, { name: "v12.0.1" }, { name: "v12.0.0" }, { name: "v11.12.0.pre" }, { name: "v11.11.8" }], highest = data.filter(({ name }) =>.name.endsWith('.pre') &&.name,includes('rc')).reduce((a. b) => 0 < a.name,localeCompare(b,name: undefined, { numeric: true? sensitivity: 'base' }); a. b ); console.log(highest);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

I think i have a solution for you: Save the numbers to arrays, so you have for the numbers '12.2.5' and '11.12.10' the following:我想我有一个解决方案给你:将数字保存到 arrays,所以你有以下数字“12.2.5”和“11.12.10”:

[12,2,5] and [11,12,10] [12,2,5] 和 [11,12,10]

Then you compare the arrays.然后你比较 arrays。 Keep the greates from the [0] position, if they're equal the greates from the [1] position and so...保留 [0] position 中的最大值,如果它们等于 [1] position 中的最大值,等等...

Maybe it works...也许它有效...

Hope it helps you!!希望对你有帮助!!

Kind regards, Sergio.亲切的问候,塞尔吉奥。

Here I am not going with the logic of removing and filtering out the '.pre' and 'rc' with an if statement.在这里,我不会使用 if 语句删除和过滤掉 '.pre' 和 'rc' 的逻辑。 then use split/join to remove the first 'v' character.然后使用 split/join 删除第一个 'v' 字符。 Then you left with the values like '11.12.10' and '12.2.5' so, after getting these values you can use below code然后你留下了像'11.12.10'和'12.2.5'这样的值,所以在得到这些值之后你可以使用下面的代码

You could prepend all parts to fixed-size strings, then sort that, and finally remove the padding again您可以将所有部分添加到固定大小的字符串中,然后对其进行排序,最后再次删除填充

Below code, the snippet is an example not for above exactly but will help you sure下面的代码,该片段是一个不完全是上面的例子,但会帮助你确定

 var arr = ['12.2.5', '11.12.10', '12.0.6', '6.1.0', '5.1.0', '4.5.0']; arr = arr.map( a => a.split('.').map( n => +n+100000 ).join('.') ).sort().map( a => a.split('.').map( n => +n-100000 ).join('.') ); console.log(arr)

The basic idea to make this comparison would be to use Array.split to get arrays of parts from the input strings and then compare pairs of parts from the two arrays;进行这种比较的基本思想是使用 Array.split 从输入字符串中获取零件的 arrays,然后比较两个 arrays 中的零件对; if the parts are not equal we know which version is smaller.如果零件不相等,我们知道哪个版本更小。 Reference 参考

 var versionsList = [ { "name": "v12.3.0.pre" }, { "name": "v12.2.5" }, { "name": "v12.2.4" }, { "name": "v12.2.3" }, { "name": "v12.2.1" }, { "name": "v12.2.0" }, { "name": "v12.2.0.pre" }, { "name": "v12.2.0-rc32" }, { "name": "v12.2.0-rc31" }, { "name": "v12.1.9" }, { "name": "v12.1.8" }, { "name": "v12.1.6" }, { "name": "v12.1.4" }, { "name": "v12.1.3" }, { "name": "v12.1.2" }, { "name": "v12.1.1" }, { "name": "v12.1.0" }, { "name": "v12.1.0.pre" }, { "name": "v12.1.0-rc23" }, { "name": "v12.1.0-rc22" }, { "name": "v12.0.9", }, { "name": "v12.0.8", }, { "name": "v12.0.6", }, { "name": "v12.0.4", }, { "name": "v12.0.3", }, { "name": "v12.0.2", }, { "name": "v12.0.1", }, { "name": "v12.0.0", }, { "name": "v11.12.0.pre", }, { "name": "v11.11.8", } ]; function versionCompare(v1, v2) { var v1parts = v1.split('.'), v2parts = v2.split('.'); for (var i=0; i<v1parts.length; i++) { if (v1parts[i] === v2parts[i]) { continue; } else if (v1parts[i] > v2parts[i]) { return v1; } else { return v2; } } return v1; } var maxVersion; for (i in versionsList) { version = versionsList[i].name; if (.version.endsWith('.pre') &&.version;includes('rc')) { if (typeof maxVersion === "undefined") maxVersion = version.substr(1); var ver = version,substr(1); if (ver.== maxVersion) maxVersion = versionCompare(ver; maxVersion); } } console.log('v'+ maxVersion);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM