在以下情况下,我想检测数组1中的值是否与array2相同:
Error: BabelPluginRemoveGraphQL: String interpolations are not allowed
in graphql fragments. Included fragments should be referenced as
`...MyModule_foo`.
到目前为止,我已经明白了:
var array1 = ["Bob", "Jason", "Fred"];
var array2 = ["Bob", "Jason", "Fred"]; // result: true, expected: true
or
var array1 = ["Bob", "Jason", "Fred"];
var array2 = ["Bob", "Fred", "Jason"]; // result: false, expected: False
or
var array1 = ["Bob", "Jason", "Fred"];
var array2 = ["Bob", "Jason"]; // result: true, expected: True
or
var array1 = ["Jason", "Fred", "Bob"];
var array2 = ["Bob", "Jason"]; // result: false, expected: False
or
var array1 = ["Jason", "Bob"];
var array2 = ["Jason", "Sue", "Bob"]; // result: false, expected: True - just because array 2 contains sue and array 1 doesn't, doesn't mean jason and bob aren't in the right order. They are. We need to ignore the fact sue is interrupting them.
or
var array1 = ["Jason", "Sue", "Bob"];
var array2 = ["Jason", "Bob", "Sue"]; // result: false, expected: False
or
var array1 = ["Sue", "Bob"];
var array2 = ["Jason", "Bob", "Sue"]; // result: false, expected: False
or
var array1 = ["Bob", "Sue"];
var array2 = ["Jason", "Bob", "Sue"]; // result: false, expected: True - just because jason is playing third wheel doesn't mean bob and sue aren't in the correct order. they are. we need to ignore jason.
or
var array1 = ["Bob", "Sue", "Bob"];
var array2 = ["Bob", "Bob", "Sue"]; // result: false, expected: False
or
var array1 = ["Bob", "Sue", "Bob"];
var array2 = ["Bob", "Sue", "Bob"]; // result: true, expected: true
or
var array1 = ["Bob", "Sue", "Bob"];
var array2 = ["Sue", "Bob"]; // result: false, expected: true - in this scenario, we have two Bobs. while Bob followed by Sue is false, sue followed by bob is true. we need to ignore the first Bob.
我的问题是,以上内容并非始终都能产生预期的结果。也就是说,如果两个数组的长度相同,那么我会得到预期的结果,但是如果不是,则不会得到预期的结果。
我需要忽略缺失或增加的值,而纯粹查看它们是否具有相同的顺序,尽管这些缺失或增加的值。
答案 0 :(得分:2)
myArray
是一个不好的名字,因为它不是数组。它是两个数组共享的最小长度,因此将其命名为minLength
左右。然后您就可以遍历索引为i
的所有条目,直到minLength
,但是minLength[i]
没有任何意义。相反,您想在数组中查找,例如array1[i]
和array2[i]
并比较结果(与===
)。
有了这些信息,您应该能够以自己的方式解决它:)
我该怎么做:
const result = array1.every((el, i) => i >= array2.length || el === array2[i]);
答案 1 :(得分:0)
一种可能是用两个字符串中都没有的分隔符来.join
两个数组,然后检查较长的.includes
是否较短:
const array1 = ["Jason", "Bob"];
const array2 = ["Jason", "Bob", "Sue"];
const str1 = array1.join('_');
const str2 = array2.join('_');
console.log(
str1.length > str2.length
? str1.includes(str2)
: str2.includes(str1)
);
答案 2 :(得分:0)
您可以检查数组最小长度的切片的元素。
function sameOrder(a, b) {
return a
.slice(0, Math.min(a.length, b.length))
.every((v, i) => v === b[i]);
}
console.log(sameOrder(["Bob", "Jason", "Fred"], ["Bob", "Jason", "Fred"])); // true
console.log(sameOrder(["Bob", "Jason", "Fred"], ["Bob", "Fred", "Jason"])); // false
console.log(sameOrder(["Bob", "Jason", "Fred"], ["Bob", "Jason"])); // true
console.log(sameOrder(["Jason", "Fred", "Bob"], ["Bob", "Jason"])); // false
console.log(sameOrder(["Jason", "Bob"], ["Jason", "Sue", "Bob"])); // false
console.log(sameOrder(["Jason", "Bob"], ["Jason", "Bob", "Sue"])); // true
答案 3 :(得分:0)
const sameSequence = (array1, array2) => {
const longest = array1.length > array2.length ? array1 : array2;
const shortest = array1.length <= array2.length ? array1 : array2;
return shortest.every((item, index) => item === longest[index]);
};
array1 = console.log(sameSequence(["Bob", "Jason", "Fred"], ["Bob", "Jason", "Fred"]));
array1 = console.log(sameSequence(["Bob", "Jason", "Fred"], ["Bob", "Fred", "Jason"]));
array1 = console.log(sameSequence(["Bob", "Jason", "Fred"], ["Bob", "Jason"]));
array1 = console.log(sameSequence(["Jason", "Fred", "Bob"], ["Bob", "Jason"]));
array1 = console.log(sameSequence(["Jason", "Bob"], ["Jason", "Sue", "Bob"]));
array1 = console.log(sameSequence(["Jason", "Bob"], ["Jason", "Bob", "Sue"]));
答案 4 :(得分:0)
如果我继续使用您的方法,则以下功能有效
function matchArrays(arr1, arr2){
//First result is true
var result= true;
//Get minimum length
var iteration= Math.min(arr1.length, arr2.length);
//For loop
for(var i = 0; i<iteration;i++){
//if not matched
if(arr1[i]!=arr2[i]){
result=false;
break;
}
}
return result;
}
array1 = ["Bob", "Jason", "Fred"];
array2 = ["Bob", "Jason", "Fred"]; // true
document.write(matchArrays(array1, array2));
document.writeln("<br>");
//or
array1 = ["Bob", "Jason", "Fred"];
array2 = ["Bob", "Fred", "Jason"]; // False
document.write(matchArrays(array1, array2));
document.writeln("<br>");
//or
array1 = ["Bob", "Jason", "Fred"];
array2 = ["Bob", "Jason"]; // True
document.write(matchArrays(array1, array2));
document.writeln("<br>");
//or
array1 = ["Jason", "Fred", "Bob"];
array2 = ["Bob", "Jason"]; // False
document.write(matchArrays(array1, array2));
document.writeln("<br>");
//or
array1 = ["Jason", "Bob"];
array2 = ["Jason", "Sue", "Bob"]; // False
document.write(matchArrays(array1, array2));
document.writeln("<br>");
//or
array1 = ["Jason", "Bob"];
array2 = ["Jason", "Bob", "Sue"]; // True
document.write(matchArrays(array1, array2));
document.writeln("<br>");
function matchArrays(arr1, arr2){
var result= true;
var iteration= Math.min(arr1.length, arr2.length);
for(var i = 0; i<iteration;i++){
if(arr1[i]!=arr2[i]){
result=false;
break;
}
}
return result;
}
答案 5 :(得分:0)
本节包含我与OP(原始海报)之间的讨论摘要。
首先,我们像这样重新定义问题:
ME:那么您想比较常用元素的顺序吗?
OP:如果是那样的话,是的。请记住,名称可以在一个或两个数组中重复。我们正在尝试检测是否发生了反转,即使可能存在无关紧要的元素。
然后,我们将问题简化为比较两个单词之间的常见字母顺序:
OP: [剪切]“ AB” ==“ ACB”,“ AB” ==“ ABC”,“ AB” ==“ CAB”,“ AB”!==“ BCA “,” AB“!==” CBA“,” AB“!==” BAC“
最后,OP发布了以下建议:
OP:我认为最简单的方法是检测那些未出现在另一个数组中的元素并将其删除。以相反的方向进行操作,然后进行比较(删除C)。我只是不知道该怎么做。
我还没有尝试过,但是我认为它与下面的第一个算法非常接近,因为它只会忽略不在另一个数组中的元素。
这个想法是从左到右读取数组,删除成对的公共元素,然后检查剩余集中是否还有公共元素。
警告。失败,显示“ ABACD”和“ BADCD”。
注释1。该反例很有趣,因为它揭示了无论给
match
函数赋予参数的顺序如何,我们都会错过有效的一对数组。注释2。在这种情况下,以相反的方式进行迭代是有效的,但是使用回文法,我们可以证明它并不总是有用,例如“ ABACDDCABA”和“ BADCDDCDAB”。
注释3。如果我们删除“ ABACD”的首字母,该算法将给出预期结果,这表明递归方法可能是合适的。
一张图片值一千字:
a = "ABA", b = "BA" => a[i] != b[j] => x = 0b000, y = 0b00
^ ^ ^ ^
i j i j
a = "ABA", b = "BA" => a[i] == b[j] => x = 0b001, y = 0b10
^ ^ ^ ^
i j i j
> | a.split("").filter(function (_, i) {
| return bit(i, x) === 0;
| })
< | ["B", "A"]
> | b.split("").filter(function (_, i) {
| return bit(i, y) === 0;
| })
< | ["B"]
如果最终数组包含公共元素,我们会说存在一个反转。在这种情况下,我们必须交换原始数组以再次比较它们:
a = "BA", b = "ABA" => a[i] != b[j] => x = 0b00, y = 0b000
^ ^ ^ ^
i j i j
a = "BA", b = "ABA" => a[i] == b[j] => x = 0b01, y = 0b010
^ ^ ^ ^
i j i j
a = "BA", b = "ABA" => a[i] == b[j] => x = 0b11, y = 0b110
^ ^ ^ ^
i j i j
> | a.split("").filter(function (_, i) {
| return bit(i, x) === 0;
| })
< | []
> | b.split("").filter(function (_, i) {
| return bit(i, y) === 0;
| })
< | ["A"]
考虑到最后的结果,我们会说“ ABA”和“ BA”符合您的条件。
fails = 0;
N = !(Y = true)
// tests[3 * i] = expected result
// tests[3 * i + 1] = array A
// tests[3 * i + 2] = array B
tests = [
N, "BAA".split(""), "ABA".split(""),
N, "ABA".split(""), "BAA".split(""),
Y, "ABA".split(""), "BA+".split(""),
Y, "BA+".split(""), "ABA".split(""),
Y, "ABACD".split(""), "BADCD".split(""),
Y, ["Bob", "Jason", "Fred"], ["Bob", "Jason", "Fred"],
N, ["Bob", "Jason", "Fred"], ["Bob", "Fred", "Jason"],
Y, ["Bob", "Jason", "Fred"], ["Bob", "Jason"],
N, ["Jason", "Fred", "Bob"], ["Bob", "Jason"],
Y, ["Jason", "Bob"], ["Jason", "Sue", "Bob"],
N, ["Jason", "Sue", "Bob"], ["Jason", "Bob", "Sue"],
N, ["Sue", "Bob"], ["Jason", "Bob", "Sue"],
Y, ["Bob", "Sue"], ["Jason", "Bob", "Sue"],
N, ["Bob", "Sue", "Bob"], ["Bob", "Bob", "Sue"],
Y, ["Bob", "Sue", "Bob"], ["Bob", "Sue", "Bob"],
Y, ["Bob", "Sue", "Bob"], ["Sue", "Bob"]
];
for (i = 0; i < tests.length; i += 3) {
a = tests[i + 1];
b = tests[i + 2];
shouldMatch = tests[i];
doesMatch = match(a, b) || match(b, a);
if (shouldMatch !== doesMatch) fails++;
console.log(
shouldMatch ? "Y" : "N",
doesMatch ? "Y" : "N",
JSON.stringify(a),
JSON.stringify(b)
);
}
console.log(
"fails =", fails
);
function bit (i, n) {
return n >> i & 1;
}
function match (a, b) {
var offset = 0, x = 0, y = 0;
for (var i = 0; i < a.length; i++) {
for (var j = offset; j < b.length; j++) {
if (a[i] === b[j]) {
x += 1 << i;
y += 1 << j;
offset = j + 1;
j = b.length; // break
}
}
}
a = a.filter(function (_, i) {
return bit(i, x) === 0;
});
b = b.filter(function (_, i) {
return bit(i, y) === 0;
});
return !a.some(function (x) {
return b.some(function (y) {
return x === y;
});
});
}
警告。该算法的正确性必须得到验证(到目前为止没有反例)。
我们的想法是比较K取的N个元素的每种组合,其中K从N变为0。
让我们集中讨论N = 3和K = 2的特定情况。为了简化代码,我在最小的单词上加上了+
符号,以便以相同长度的单词开头:
rmDots = w => w.replace(/\.+/g, ""); // dots remover
a = "ABA"; b = "BA+"; // words
n = 3; // words length
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
// explode `a` and `b`
x = a.split("");
y = b.split("");
// hide one letter starting
// from the rightmost one
x[n - (i + 1)] = ".";
y[n - (j + 1)] = ".";
// implode `a` and `b`
x = x.join("");
y = y.join("");
// print out
console.log(
// match ? "Yes" : "No"
rmDots(x) === rmDots(y) ? "Y" : "N",
JSON.stringify(x), JSON.stringify(y)
);
}
}
此后,您必须检查匹配的组合对,以过滤没有剩余通用字母的组合。例如,使用“ ABA”和“ BAA”,以及K = 2(并且N = 3,因为N是字符串的长度),您将获得3对匹配的组合:
Y "A.A" ".AA"
Y ".BA" "BA."
Y ".BA" "B.A"
但是,总是总是有一个剩余的共同字母(在圆点后面),分别是".B."
和"B.."
,“ A.."
和"..A"
和{{1 }}和"A.."
。因此,在K = 2的情况下,实际上没有一对符合您条件的组合,因此您必须在K = 1的情况下再试一次。
以下代码段应有助于理解最终算法:
".A."
function C (n, k) {
var acc = 1, i = 0;
while (++i <= k) acc *= (n - k + i) / i;
return acc;
}
function Ci (n, k, i) {
var j, c, flags = new Array(n);
for (j = 1; j <= n; j++) {
if (k > 0 && (c = C(n - j, k - 1)) > i) {
k -= 1; flags[j - 1] = true;
} else {
i -= c; flags[j - 1] = false;
}
}
return flags;
}
/* ignore this line */ (function(){for(var n=/^ */,e=">",t="<",r="!",o="+",i=Array.prototype.map,l=Array.prototype.slice,a=document.getElementsByTagName("pre"),u=0,c=arguments.length;u<c;u++)a[u].innerHTML=i.call(arguments[u],function(n){return p(n[0])+f(n[2])+s(n[1])}).join("");function p(t){var r=t.split("\n"),o=r[0].match(n)[0].length;return y(e,d(r.map(function(n){return n.slice(o)}).join("\n")))}function s(n){return n instanceof Error?y(r,g("#F00",n+"")):y(t,void 0===n?g("#999","undefined"):d(JSON.stringify(n)))}function f(n){return n.reduce(function(n,e){var t="string"!=typeof e[0],r=l.call(e).map(function(n){return"string"!=typeof n||t?JSON.stringify(n):n}).join(" ");return n+y(o,t?d(r):r)},"")}function y(n,e){return'<span style="display:block"><span style="display:inline-block">'+e.split("\n").map(function(e,t){return(0===t?n:" ")+" | "}).join("\n")+'</span><span style="display:inline-block">'+e+"</span></span>"}function g(n,e){return"<span "+('style="color:'+n+'"')+">"+e+"</span>"}function d(n){return"<code>"+n+"</code>"}}).apply(this,eval("["+function(){var n=/("|\\)/g,e=/^.*?\n|\n.*?$/g,t=Array.prototype.map,r=Array.prototype.filter,o=document.getElementsByTagName("pre");return t.call(o,function(t){return"["+r.call(t.childNodes,function(n){return 8===n.nodeType&&n.nodeValue.split("\n").length>2}).map(function(t){return["function(b,i,o){","return console.log=b[0],[","i,o,b[1]","];","}(function(f,l){","return console.log=function(){","return l.push(arguments),(","f.apply(console,arguments)",");","},[f,l];","}(console.log,[]),",t=JSON.stringify(t.nodeValue.replace(e,"")),',eval("try{',"eval(",t.replace(n,"\\$1"),")",'}catch(e){e}"))'].join("")}).join(",")+"]"}).join(",")}()+"]"));
/* ignore this line */ body{padding:1em !important}html,body{min-width:auto !important}
请记住,这是蛮力方法。每个K都有C(n,k)* C(n,k)个可能的组合对(有关C(n,k)的详细信息,请参见Wikipedia),因此,比较关于字符串(n)的大小,字符串可能呈指数增长(C(n,k)^ 2)。换句话说,大字符串可能会耗尽您的CPU ...
<link href="https://cdn.sstatic.net/Shared/stacks.css?v=58428843e325" rel="stylesheet"/>
<link href="https://cdn.sstatic.net/Sites/stackoverflow/primary.css?v=2ed743cc91af" rel="stylesheet"/>
<link href="https://cdn.sstatic.net/clc/styles/clc.min.css?v=768595a6d237" rel="stylesheet"/>
<blockquote>
<p><strong>Help.</strong> Run this snippet then press "Full page".</p>
</blockquote>
<blockquote>
<p><strong>Help.</strong> <code>></code> = "input", <code><</code> = "output", <code>+</code> = "log".</p>
</blockquote>
<p>Taking 2 things among 3:</p>
<pre>
<!--
n = 3
--><!--
k = 2
--><!--
Ci(n, k, 0) // 1st combination
--><!--
Ci(n, k, 2) // 3rd combination
-->
</pre>
<p>Enumerating combinations:</p>
<pre>
<!--
c = C(n, k) // how many combinations?
--><!--
for (i = 0; i < c; i++) {
console.log(i, Ci(n, k, i));
}
-->
</pre>
<p>Hiding 2 letters among 3:</p>
<pre>
<!--
letters = "ABC".split("")
--><!--
for (i = 0; i < c; i++) {
flags = Ci(n, k, i);
console.log(i, letters.map(function (letter, i) {
var hide = flags[i];
return hide ? "." : letter;
}).join(""), flags);
}
-->
</pre>
<p>Taking 2 letter among 3:</p>
<pre>
<!--
letters = "XYZ".split("")
--><!--
for (i = 0; i < c; i++) {
flags = Ci(n, k, i);
console.log(i, letters.filter(function (_, i) {
var take = flags[i];
return take;
}).join(""), flags);
}
-->
</pre>
请注意,我用fails = 0;
N = !(Y = true);
// tests[3 * i] = expected result
// tests[3 * i + 1] = array A
// tests[3 * i + 2] = array B
tests = [
N, "BAA".split(""), "ABA".split(""),
N, "ABA".split(""), "BAA".split(""),
Y, "ABA".split(""), "BA+".split(""),
Y, "BA+".split(""), "ABA".split(""),
Y, "ABACD".split(""), "BADCD".split(""),
Y, "ABACDDCABA".split(""), "BADCDDCDAB".split(""),
Y, ["Bob", "Jason", "Fred"], ["Bob", "Jason", "Fred"],
N, ["Bob", "Jason", "Fred"], ["Bob", "Fred", "Jason"],
Y, ["Bob", "Jason", "Fred"], ["Bob", "Jason"],
N, ["Jason", "Fred", "Bob"], ["Bob", "Jason"],
Y, ["Jason", "Bob"], ["Jason", "Sue", "Bob"],
N, ["Jason", "Sue", "Bob"], ["Jason", "Bob", "Sue"],
N, ["Sue", "Bob"], ["Jason", "Bob", "Sue"],
Y, ["Bob", "Sue"], ["Jason", "Bob", "Sue"],
N, ["Bob", "Sue", "Bob"], ["Bob", "Bob", "Sue"],
Y, ["Bob", "Sue", "Bob"], ["Bob", "Sue", "Bob"],
Y, ["Bob", "Sue", "Bob"], ["Sue", "Bob"]
];
for (i = 0; i < tests.length; i += 3) {
a = tests[i + 1];
b = tests[i + 2];
shouldMatch = tests[i];
doesMatch = match(a, b);
if (shouldMatch !== doesMatch) fails++;
console.log(
shouldMatch ? "Y" : "N",
doesMatch ? "Y" : "N",
JSON.stringify(a),
JSON.stringify(b)
);
}
console.log(
"fails =", fails
);
function C (n, k) {
var acc = 1, i = 0;
while (++i <= k) acc *= (n - k + i) / i;
return acc;
}
function Ci (n, k, i) {
var j, c, flags = new Array(n);
for (j = 1; j <= n; j++) {
if (k > 0 && (c = C(n - j, k - 1)) > i) {
k -= 1; flags[j - 1] = true;
} else {
i -= c; flags[j - 1] = false;
}
}
return flags;
}
function match (a, b) {
var n, c, i, j;
var k; // drop `k` elements
var a2, b2, a3, b3, aFlags, bFlags;
n = Math.max(a.length, b.length);
if (a.length < n) a = a.concat(
new Array(n - a.length).fill(null)
);
if (b.length < n) b = b.concat(
new Array(n - b.length).fill(null)
);
for (k = 0; k < n; k++) {
c = C(n, k);
for (i = 0; i < c; i++) {
aFlags = Ci(n, k, i);
a2 = a.filter(function (_, i) {
return !aFlags[i];
});
for (j = 0; j < c; j++) {
bFlags = Ci(n, k, j);
b2 = b.filter(function (_, i) {
return !bFlags[i];
});
// a2[i] = b2[i] (i in [0-(n-k)])
// means that we found the biggest
// sequence of common elements.
// Therefore, we can stop searching
// and check if there is at least
// one pair of common elements
// in the remaining set.
if (a2.every(function (x, i) {
return x === b2[i];
})) {
a3 = a.filter(function (_, i) {
return aFlags[i];
});
b3 = b.filter(function (_, i) {
return bFlags[i];
});
return !a3.some(function (x) {
return b3.some(function (y) {
return x === y;
});
});
}
}
}
}
return false;
}
值填充了最小的数组,以相同长度的数组开始。如果数组已经包含null
值,则可能是个问题,但是找到解决方法并不重要。
答案 6 :(得分:0)
此代码将帮助您找到数组的顺序。 如果数组顺序相同,函数将返回true并返回 如果两个数组均未按顺序排列,则为false。
let v = ['bathri','nathan',2];
let x = ['bathri','nathan'];
let value = [];
let checkOrder = function(one, two){
if(one.length != two.length){
value = one.length < two.length ? one : two;
}
else{
value = one;
}
for(let i = 0; i < value.length; i++){
if(v[i] != x[i])
{
return false;
break;
}
}
return true;
}
console.log(checkOrder(v,x));
感谢您的贡献,但无济于事:-\
N = !(Y = true);
// tests[3 * i] = expected result
// tests[3 * i + 1] = array A
// tests[3 * i + 2] = array B
tests = [
N, "BAA".split(""), "ABA".split(""),
N, "ABA".split(""), "BAA".split(""),
Y, "ABA".split(""), "BA+".split(""),
Y, "BA+".split(""), "ABA".split(""),
Y, "ABACD".split(""), "BADCD".split(""),
Y, "ABACDDCABA".split(""), "BADCDDCDAB".split(""),
Y, ["Bob", "Jason", "Fred"], ["Bob", "Jason", "Fred"],
N, ["Bob", "Jason", "Fred"], ["Bob", "Fred", "Jason"],
Y, ["Bob", "Jason", "Fred"], ["Bob", "Jason"],
N, ["Jason", "Fred", "Bob"], ["Bob", "Jason"],
Y, ["Jason", "Bob"], ["Jason", "Sue", "Bob"],
N, ["Jason", "Sue", "Bob"], ["Jason", "Bob", "Sue"],
N, ["Sue", "Bob"], ["Jason", "Bob", "Sue"],
Y, ["Bob", "Sue"], ["Jason", "Bob", "Sue"],
N, ["Bob", "Sue", "Bob"], ["Bob", "Bob", "Sue"],
Y, ["Bob", "Sue", "Bob"], ["Bob", "Sue", "Bob"],
Y, ["Bob", "Sue", "Bob"], ["Sue", "Bob"]
];
for (i = 0; i < tests.length; i += 3) {
a = tests[i + 1];
b = tests[i + 2];
shouldMatch = tests[i];
doesMatch = checkOrder(a, b);
console.log(
shouldMatch ? "Y" : "N",
doesMatch ? "Y" : "N",
JSON.stringify(a),
JSON.stringify(b)
);
}
function checkOrder (one, two) {
if (one.length != two.length) {
value = one.length < two.length ? one : two;
} else {
value = one;
}
for (let i = 0; i < value.length; i++) {
if (one[i] != two[i]) {
return false;
break;
}
}
return true;
}