我有一个字符串数组,其中包含文件名以及Year_Program_Version的组合
例如
var fileNames = [
"2021_Test_V1",
"2021_Test_V2",
"2021_Test_V4",
"2021_Test_V3",
"2020_Test_V11",
"2020_Test_V10"
];
正在寻找具有2021_Test_V4、2020_Test_V11的输出数组
答案 0 :(得分:2)
使用所有年份/程序作为键并使用其最新版本作为值来构建对象,然后映射回文件名数组:
const fileNames = [
"2021_Test_V1",
"2021_Test_V2",
"2021_Test_V4",
"2021_Test_V3",
"2020_Test_V11",
"2020_Test_V10"
];
const latestFileNames = Object.entries(fileNames.reduce((a, v) => {
const [yp, version] = v.split(/_V(?=\d+$)/);
a[yp] = a[yp] && +a[yp] >= +version ? a[yp] : version;
return a;
}, {})).map(([yp, version]) => `${yp}_V${version}`);
console.log(latestFileNames)
答案 1 :(得分:0)
我将从创建一个包含文件名前缀和最新版本(数字方式)的地图开始。
然后您可以将这些条目映射回其完整的文件名。
例如
Equals
答案 2 :(得分:0)
执行此操作的方式略有不同:
const fileNames = [
"2021_Test_V1",
"2021_Test_V2",
"2021_Test_V4",
"2021_Test_V3",
"2020_Test_V11",
"2020_Test_V10"
]
var lastYear = -1;
var lastVersion = -1;
var years={};
fileNames.forEach(function(file){
var year = file.split("_")[0];
var version = parseInt(file.split("_")[2].split("V")[1]);
if(year != lastYear){
lastYear = year;
lastVersion = -1;
}
if(version > lastVersion){
lastVersion = version;
years[year] = file;
}
});
console.log(Object.values(years));
返回:
["2020_Test_V11", "2021_Test_V4"]
答案 3 :(得分:0)
一种可能的解决方案(性能可能不太好)是将Array.filter()与嵌套的Array.some()配合使用,以仅保留满足预期条件的元素。这也假设所有年份都使用四位数格式。
var fileNames = [
"2021_Test_V1",
"2021_Test_V2",
"2021_Test_V4",
"2021_Test_V3",
"2021_Test_V10",
"2020_Test_V11",
"2020_Test_V10",
"2019_Test_V1"
];
let res = fileNames.filter((str, i, arr) =>
{
let [year, app, version] = str.split("_");
return !arr.some(s =>
{
let [y, a, v] = s.split("_");
return y === year && +v.slice(1) > +version.slice(1);
});
});
console.log(res);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}
答案 4 :(得分:0)
基于Robby Cornelissen的答案,但是对于ECMASCRIPT3,我知道它很旧,但是据我所知,它是Adobe Animate CC 2019 JS编程API唯一了解的版本:-(。
借助Closure:
java -jar closure-compiler\closure-compiler-v20200406.jar --language_in ECMASCRIPT_2015 --language_in ECMASCRIPT3 --js test.js --js_output_file test_es3.js --compilation_level BUNDLE
结果代码:
var fileNames = [
"2021_Test_V1",
"2021_Test_V2",
"2021_Test_V4",
"2021_Test_V3",`enter code here`
"2020_Test_V11",
"2020_Test_V10"
];
function e(a) {
var b = 0;
return function() {
return b < a.length ? {done:!1, value:a[b++]} : {done:!0};
};
}
function f(a) {
var b = "undefined" != typeof Symbol && Symbol.iterator && a[Symbol.iterator];
return b ? b.call(a) : {next:e(a)};
}
// From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
if (!Object.keys) {
Object.keys = (function() {
'use strict';
var hasOwnProperty = Object.prototype.hasOwnProperty,
hasDontEnumBug = !({ toString: null }).propertyIsEnumerable('toString'),
dontEnums = [
'toString',
'toLocaleString',
'valueOf',
'hasOwnProperty',
'isPrototypeOf',
'propertyIsEnumerable',
'constructor'
],
dontEnumsLength = dontEnums.length;
return function(obj) {
if (typeof obj !== 'function' && (typeof obj !== 'object' || obj === null)) {
throw new TypeError('Object.keys called on non-object');
}
var result = [], prop, i;
for (prop in obj) {
if (hasOwnProperty.call(obj, prop)) {
result.push(prop);
}
}
if (hasDontEnumBug) {
for (i = 0; i < dontEnumsLength; i++) {
if (hasOwnProperty.call(obj, dontEnums[i])) {
result.push(dontEnums[i]);
}
}
}
return result;
};
}());
}
Array.prototype.getLastVersion = function(){
var filenames = this.toString();
var g = Object.entries(filenames.split(",").reduce(function(a, b) {
var c = f(b.split(/_V(?=\d+$)/)), d = c.next().value;
c = c.next().value;
a[d] = a[d] && +a[d] >= +c ? a[d] : c;
return a;
}, {})).map(function(a) {
var b = f(a);
a = b.next().value;
b = b.next().value;
return a + "_V" + b;
});
return g;
}
console.log(fileNames.getLastVersion());