JS比较两个不同数组中某些元素的顺序
JS compare the order of SOME elements in two different arrays
我想知道如何比较两个数组中的某些元素是否以相同的顺序进行比较。
var compare = function (arr1, arr2) {
//........
}
compare ([f,t,r,m], [s,f,t,r,q,p,m]); //should return true
compare ([f,t,r,m], [f,a,t,,m,r]); //should return false
我进行了一个 for 循环,然后确定值何时匹配,然后我很确定你应该比较数组,但我觉得我遗漏了一些东西。
var compare = function (a, b) {
a.forEach(function(letter){
for (i=0; i<b.length; i++) {
if (letter===b[i]) {}
}
})
}
使用 Array#filter and Array#indexOf. Then iterate the result with Array#every 从第二个数组中删除所有未出现在第一个数组中的字母,并检查每个字符是否出现在第一个数组中的相同位置:
function compare(a, b) {
var arr = b.filter(function(c) {
return a.indexOf(c) !== -1; // use a hash object instead of indexOf if the arrays are large
});
return arr.every(function(c, i) {
return c === a[i];
});
}
console.log(compare(['f','t','r','m'], ['s','f','t','r','q','p','m'])); //should return true
console.log(compare(['f','t','r','m'], ['f','a','t','m','r'])); //should return false
这两个函数都将与 O(n)
运行时进行比较,Ori Drori 的解决方案在 O(n^2)
中运行
var a = [1,2,3];
var b = [0,1,4,3,9,10,2,5,6]; // 1,2,3 in wrong order
var c = [0,4,1,5,6,2,8,3,5]; // 1,2,3 in right order
// using foreach
function compare(a,b){
var i = 0;
b.forEach(function(el){
if(el == a[i]) i++;
})
return i == a.length;
}
// using reduce
function compare2(a,b){
return b.reduce(function(i, el){
return el == a[i] ? i + 1 : i;
}, 0) == a.length;
}
console.log(compare(a,b) == false); // should be false
console.log(compare(a,c) == true); // should be true
console.log(compare2(a,b) == false); // should be false
console.log(compare2(a,c) == true); // should be true
您可以为 array2
取一个索引并在迭代时检查 return 索引与 array2
的比较以及 array1
的元素。
function compare(array1, array2) {
var i = 0;
return array1.every(function (a) {
while (i < array2.length && a !== array2[i]) {
i++;
}
return a === array2[i++];
});
}
console.log(compare(['f', 't', 'r', 'm'], ['s', 'f', 't', 'r', 'q', 'p', 'm'])); // true
console.log(compare(['f', 't', 'r', 'm'], ['f', 'a', 't', , 'm', 'r'])); // false
您可以进行以下操作;
function compare(a,b){
return b.filter(e => a.includes(e))
.every((e,i) => e === a[i])
}
console.log(compare(["f","t","r","m"], ["s","f","t","r","q","p","m"]));
console.log(compare(["f","t","r","m"], ["f","a","t","m","r"]));
我想知道如何比较两个数组中的某些元素是否以相同的顺序进行比较。
var compare = function (arr1, arr2) {
//........
}
compare ([f,t,r,m], [s,f,t,r,q,p,m]); //should return true
compare ([f,t,r,m], [f,a,t,,m,r]); //should return false
我进行了一个 for 循环,然后确定值何时匹配,然后我很确定你应该比较数组,但我觉得我遗漏了一些东西。
var compare = function (a, b) {
a.forEach(function(letter){
for (i=0; i<b.length; i++) {
if (letter===b[i]) {}
}
})
}
使用 Array#filter and Array#indexOf. Then iterate the result with Array#every 从第二个数组中删除所有未出现在第一个数组中的字母,并检查每个字符是否出现在第一个数组中的相同位置:
function compare(a, b) {
var arr = b.filter(function(c) {
return a.indexOf(c) !== -1; // use a hash object instead of indexOf if the arrays are large
});
return arr.every(function(c, i) {
return c === a[i];
});
}
console.log(compare(['f','t','r','m'], ['s','f','t','r','q','p','m'])); //should return true
console.log(compare(['f','t','r','m'], ['f','a','t','m','r'])); //should return false
这两个函数都将与 O(n)
运行时进行比较,Ori Drori 的解决方案在 O(n^2)
中运行
var a = [1,2,3];
var b = [0,1,4,3,9,10,2,5,6]; // 1,2,3 in wrong order
var c = [0,4,1,5,6,2,8,3,5]; // 1,2,3 in right order
// using foreach
function compare(a,b){
var i = 0;
b.forEach(function(el){
if(el == a[i]) i++;
})
return i == a.length;
}
// using reduce
function compare2(a,b){
return b.reduce(function(i, el){
return el == a[i] ? i + 1 : i;
}, 0) == a.length;
}
console.log(compare(a,b) == false); // should be false
console.log(compare(a,c) == true); // should be true
console.log(compare2(a,b) == false); // should be false
console.log(compare2(a,c) == true); // should be true
您可以为 array2
取一个索引并在迭代时检查 return 索引与 array2
的比较以及 array1
的元素。
function compare(array1, array2) {
var i = 0;
return array1.every(function (a) {
while (i < array2.length && a !== array2[i]) {
i++;
}
return a === array2[i++];
});
}
console.log(compare(['f', 't', 'r', 'm'], ['s', 'f', 't', 'r', 'q', 'p', 'm'])); // true
console.log(compare(['f', 't', 'r', 'm'], ['f', 'a', 't', , 'm', 'r'])); // false
您可以进行以下操作;
function compare(a,b){
return b.filter(e => a.includes(e))
.every((e,i) => e === a[i])
}
console.log(compare(["f","t","r","m"], ["s","f","t","r","q","p","m"]));
console.log(compare(["f","t","r","m"], ["f","a","t","m","r"]));