Javascript命令数组匹配最接近的回退

问题描述 投票:1回答:2

我有一个问题,我正试图解决,我还没有完全能够解决问题。如果有人能指出我在这里正确的方向,我会很感激。

基本上,我在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。

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

javascript arrays comparison
2个回答
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);

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);
© www.soinside.com 2019 - 2024. All rights reserved.