[英]Javascript: How do I check if an array contains exactly another array?
Suppose I have arrays parent and child. 假设我有数组父和子。 I want to check if a child exists inside a parent array. 我想检查一个子数组是否存在于父数组中。 The ordering matters. 订购很重要。
Example: 例:
parent = ["x", "a", "b", "c", "d", "e", "f", "g"]
child = ["a", "b", "c"]
//returns true
Example: 例:
When parent has a different order: 当父母有不同的顺序时:
parent = ["x", "g", "b", "c", "d", "e", "f", "a"]
child = ["a", "b", "c"]
//It should return false
How would I achieve this in Javascript? 我如何在Javascript中实现这一目标?
Edit: I have tried this How to check if an array contains another array? 编辑:我试过这个如何检查一个数组是否包含另一个数组? but it did not work for my case 但它对我的情况不起作用
You can run a loop for child
and change the index accordingly. 您可以为child
运行循环并相应地更改索引。 You can also use a match
variable to detect change in the sequence. 您还可以使用match
变量来检测序列中的更改。
RETURN TRUE 返回
var parent = ["x", "a", "b", "c", "d", "e", "f", "g"] var child = ["a", "b", "c"]; var initIndex = parent.indexOf(child[0]); var match = true; for(var i=1; i<child.length; i++){ var varIndex = parent.indexOf(child[i]); if( varIndex === initIndex+1){ initIndex = varIndex; continue; } match = false; } console.log(match);
RETURN FALSE 返回错误
var parent = ["x", "g", "b", "c", "d", "e", "f", "a"] var child = ["a", "b", "c"]; var initIndex = parent.indexOf(child[0]); var match = true; for(var i=1; i<child.length; i++){ var varIndex = parent.indexOf(child[i]); if( varIndex === initIndex+1){ initIndex = varIndex; continue; } match = false; } //return false console.log(match);
USING STRING OPERATION 使用字符串操作
You can also convert the array to string to avoid those loop: 您还可以将数组转换为字符串以避免这些循环:
var parent = ["x", "g", "b", "c", "d", "e", "f", "a"] var child = ["a", "b", "c"] var parentStr = parent.toString(); var match = parentStr.indexOf(child.toString()) !== -1; //return false console.log(match); parent = ["x", "a", "b", "c", "d", "e", "f", "g"] child = ["a", "b", "c"] parentStr = parent.toString(); match = parentStr.indexOf(child.toString()) !== -1; //return true console.log(match);
var parent = ["x", "g", "b", "c", "d", "e", "f", "a"] var child = ["a", "b", "c"] if(parent.join("").search(child.join("")) === -1) { console.log("Not found"); } else { console.log("found") }
Convert the array to string by using JSON.stringify()
and remove the square brackets from child string. 使用JSON.stringify()
将数组转换为字符串,并从子字符串中删除方括号。
Now check the indexOf
child in parent to check if it contains the child. 现在检查父级中的indexOf
子项以检查它是否包含子级。
let parent = ["x", "a", "b", "c", "d", "e", "f", "g"]; let child = ["a", "b", "c"]; var parStr = JSON.stringify(parent); var chldStr = JSON.stringify(child).replace('[', '').replace(']', '') console.log(parStr.indexOf(chldStr) !== -1);
You could iterate parent
array and use an index for the children array and if the last child is found, return true
. 您可以迭代parent
数组并使用children数组的索引,如果找到最后一个子数,则返回true
。
function check(parent, children) { var index = 0; return parent.some(p => p === children[index] && ++index === children.length); } console.log(check(["x", "a", "b", "c", "d", "e", "f", "g"], ["a", "b", "c"])); console.log(check(["x", "g", "b", "c", "d", "e", "f", "a"], ["a", "b", "c"]));
A other approach with indexOf
indexOf
的另一种方法
function check(parent, children) { return children.every((i => c => (i = parent.indexOf(c, i)) !== -1)(0)); } console.log(check(["x", "a", "b", "c", "d", "e", "f", "g"], ["a", "b", "c"])); console.log(check(["x", "g", "b", "c", "d", "e", "f", "a"], ["a", "b", "c"]));
I have a simple method for small sized arrays of this problem. 我有一个简单的方法来解决这个问题的小型数组。
parent = ["x", "a", "b", "c", "d", "e", "f", "g"]; child = ["a", "b", "c"]; function matchSubArray(parent, child) { parentStr = parent.join(''); childStr = child.join(''); return parentStr.indexOf(childStr) != -1; } matchSubArray(parent, child);
I wrote a function for this a while back that takes some paramenters: 我为此写了一个函数,需要一些参数:
Array.prototype.containsArray = function (child, orderSensitivity, caseSensitivity, typeSensitivity) { var self = this; if (orderSensitivity) return orderSensitiveComparer(); else return orderInsensitiveComparer(); function orderSensitiveComparer() { var resultArry = [], placeholder = 0; if (child.length > self.length) return false; for (var i = 0; i < child.length; i++) { for (var k = placeholder; k < self.length; k++) { if (equalityComparer(self[k], child[i])) { resultArry.push(true); if (resultArry.length === child.length) return true; placeholder = k + 1; break; } else resultArry = []; } } return false; } function orderInsensitiveComparer() { for (var i = 0; i < child.length; i++) { var childHasParentElement = false; for (var k = 0; k < self.length; k++) { if (equalityComparer(child[i], self[k])) { childHasParentElement = true; break; } } if (!childHasParentElement) return false; } return true; } function equalityComparer(a, b) { if (caseSensitivity && typeSensitivity) return caseSensitiveEq(a, b) && typeSensitiveEq(a, b); else if (!caseSensitivity && typeSensitivity) return caseInsensitiveEq(a, b) && typeSensitiveEq(a, b); else if (caseSensitivity && !typeSensitivity) return caseSensitiveEq(a, b) && typeInsensitiveEq(a, b); else if (!caseSensitivity && !typeSensitivity) return caseInsensitiveEq(a, b) && typeInsensitiveEq(a, b); else throw "Unknown set of parameters"; function caseSensitiveEq(a, b) { return a == b; } function caseInsensitiveEq(a, b) { return (a + "").toLowerCase() == (b + "").toLowerCase(); } function typeSensitiveEq(a, b) { return typeof(a) === typeof(b); } function typeInsensitiveEq(a, b) { return true; } } } var parent = [1, 2, 3, "a", "b", "c"]; var child = [1, 2, 3]; var child2 = ["1", "2", "3"]; var child3 = ["A", "b", "C"]; var child4 = ["a", "b", "c"]; var child5 = ["c", "b", "a"]; // Tests: console.log(parent.containsArray(parent)); console.log(parent.containsArray(child)); console.log(parent.containsArray(child2)); // parent to child 2, order sensitive, not case, not type. => true. console.log(parent.containsArray(child2, true, false, false)); // parent to child 2, order, not case, type. => false. b/c of type. console.log(parent.containsArray(child2, true, false, true)); // parent to child 3, order, not case, type. => true. console.log(parent.containsArray(child3, true, false, true)); // parent to child 4, order, case and type => true. console.log(parent.containsArray(child4, true, true, true)); // parent to child 4, not order, case and type. => true. console.log(parent.containsArray(child4, false, true, true)); // parent to child 5, not order case or type => true. console.log(parent.containsArray(child5));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.