【问题标题】:Javascript ordered array match with closest fallbackJavascript有序数组匹配最接近的后备
【发布时间】:2019-01-04 17:34:31
【问题描述】:

我遇到了一个我想解决的问题,但我还没有完全解决这个问题。如果有人可以在这里为我指明正确的方向,我将不胜感激。

基本上,我是在比较 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

数组将始终保持相同的顺序,并且顺序必须匹配才能评估为真。

基本上,我试图返回最精确的匹配,但如果该匹配不存在,则提供最接近的后备。有人知道我在说什么吗?任何人可以提供的帮助将不胜感激

【问题讨论】:

  • 您可以过滤匹配并按大小对结果进行排序(最大的优先),排序后的数组中的第一个值将是最佳匹配。
  • 你遗漏了一些边缘案例 ABCE... 是否匹配 ABCD?
  • @NunoSousa,是的,我想这也会匹配。
  • ABEC 怎么样?应该匹配吗?

标签: javascript arrays comparison


【解决方案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);

【讨论】:

  • 从描述看来,OP 有多个数组,什么是找到最佳匹配的。
  • @Titus - 已更新以涵盖该案例。
【解决方案2】:
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);

【讨论】:

    猜你喜欢
    • 2019-11-02
    • 2014-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-24
    • 1970-01-01
    相关资源
    最近更新 更多