我要解决一个问题,就是我还没能力把头缠住。如果有人可以在正确的方向指点我,我将不胜感激。
基本上,我正在比较javascript中的两个有序数组。我有一个索引数组和一个要匹配的数组。如果数组中的所有元素按顺序匹配,我想返回一个匹配项。但是,我也想返回最接近的部分匹配。例如
如果我的索引数组是
var index = ['A', 'B', 'C', 'D']
我要比较的数组是
var compare = ['A', 'B', 'C', 'D']
显然应该匹配。但是这些也都应该匹配:
var compare = ['A']
var compare = ['A', 'B']
var compare = ['A', 'B', 'C']
这些不应该匹配:
var compare = ['B']; //doesn't start with 'A'
var compare = ['B', 'C']; //doesn't start with 'A'
var compare = ['B', 'A']; //not in correct order
数组将始终处于相同顺序,并且顺序必须匹配才能计算为true。
基本上,我尝试返回最精确的匹配,但如果不存在该匹配,则提供最接近的后备。有人知道我在说什么吗?人们能提供的任何帮助将不胜感激
答案 0 :(得分:1)
如果两个数组的索引处的条目匹配或,只要使用Array.prototype.every
并让回调返回true
,如果索引数组中有该索引的条目但compare
数组没有:
const flag = index.every((entry, n) => compare.length <= n || compare[n] === index[n]);
或在ES5中:
var flag = index.every(function(entry, n) {
return compare.length <= n || compare[n] === index[n];
});
实时示例(ES2015 +):
function test(index, compare, expect) {
const flag = index.every((entry, n) => compare.length <= n || compare[n] === index[n]);
console.log(index.join(","), compare.join(","), ":", flag, "=>", !flag === !expect ? "Good" : "ERROR");
}
const index = ['A', 'B', 'C', 'D'];
test(index, ['A', 'B', 'C', 'D'], true);
test(index, ['A'], true);
test(index, ['A', 'B'], true);
test(index, ['A', 'B', 'C'], true);
test(index, ['B'], false); //doesn't start with 'A'
test(index, ['B', 'C'], false); //doesn't start with 'A'
test(index, ['B', 'A'], false); //not in correct order
如果作为Titus suggests,您有一个数组数组,并且想找到最佳匹配项,则循环遍历它们并记住最长的匹配项:
let match = null;
for (const compare of arrayOfArraysToCompare) {
// No need to compare ones that are shorter than a known match...
if (!match || compare.length > match.length) {
const flag = index.every((entry, n) => compare.length <= n || compare[n] === index[n]);
if (flag && (!match || match.length < compare.length)) {
match = compare;
}
}
}
或在ES5
中var match = null;
arrayOfArraysToCompare.forEach(function(compare) {
// No need to compare ones that are shorter than a known match...
if (!match || compare.length > match.length) {
var flag = index.every((entry, n) => compare.length <= n || compare[n] === index[n]);
if (flag && (!match || match.length < compare.length)) {
match = compare;
}
}
});
实时示例(ES2015 +):
const index = ['A', 'B', 'C', 'D'];
const arrayOfArraysToCompare = [
['A'], // Match, but not longest match
['A', 'B'], // *** Longest match
['B', 'C', 'D'] // Longer, but not a match
];
let match = null;
for (const compare of arrayOfArraysToCompare) {
// No need to compare ones that are shorter than a known match...
if (!match || compare.length > match.length) {
const flag = index.every((entry, n) => compare.length <= n || compare[n] === index[n]);
if (flag && (!match || match.length < compare.length)) {
match = compare;
}
}
}
console.log(match);
答案 1 :(得分:0)
var index = ['A', 'B', 'C', 'D'];
var compare = ['A', 'B', 'C', 'D'];
function getMatches(array1, array2){
var matches = [];
array1.forEach((element, index) => {
if(element == array2[index])
matches.push(element);
else
return matches;
});
return matches;
}
getMatches(index, compare);