[英]Looking for matches in multi-dimensional arrays inJavascript
I am trying to match entire arrays in Javascript. 我试图在Javascript中匹配整个数组。 I have a userInput array and I need to find a matching array within a multi-dimensional array and output the match.
我有一个userInput数组,我需要在多维数组中找到匹配的数组并输出匹配。
var t1 = [0,0,0];
var t2 = [1,0,0];
var t3 = [0,0,1];
var userInput = [0,0,0];
var configs = [t1, t2, t3];
I am trying to find a way to match userInput to one of the other arrays and output the matching array. 我试图找到一种方法将userInput匹配到其他数组之一并输出匹配的数组。 With underscore.js I can find a match one at a time without looping, but that returns a bool.
使用underscore.js我可以一次找到一个匹配而不循环,但返回一个bool。
var result = _.isEqual(userInput,t1) returns true
I need to find the matching array inside the configs array. 我需要在configs数组中找到匹配的数组。 I can nest loops to go through configs, but I can't figure out how to match it to userInput.
我可以嵌套循环来完成配置,但我无法弄清楚如何将它与userInput匹配。
You can use Array#findIndex
to find the index of the matching config in the array. 您可以使用
Array#findIndex
查找数组中匹配配置的索引。 Use Array#every
to find the config that is equal. 使用
Array#every
查找相同的配置。
var t1 = [0,0,0]; var t2 = [1,0,0]; var t3 = [0,0,1]; var userInput = [0,0,1]; // this will fit t3 (index 2) var configs = [t1, t2, t3]; var result = configs.findIndex(function(arr) { return arr.every(function(value, i) { return value === userInput[i]; }); }); console.log(result);
Given input you can use Array.prototype.find()
, Array.prototype.toString()
or Array.prototype.join()
给定输入,您可以使用
Array.prototype.find()
, Array.prototype.toString()
或Array.prototype.join()
var t1 = [0,0,0]; var t2 = [1,0,0]; var t3 = [0,0,1]; var userInput = [0,0,0]; var configs = [t1, t2, t3]; var result = configs.find(arr => arr.join() === userInput.join()); console.log(result);
You can use a combination of some and every 您可以使用一些和每个的组合
var t1 = [0,0,0]; var t2 = [1,0,0]; var t3 = [0,0,1]; var userInput = [0,0,0]; var configs = [t1, t2, t3]; var inputPresent = configs.some(a => userInput.every((u,i) => a[i] == u)); console.log(inputPresent);
If you wanted to know the index of the existing input, you could just include a ternary 如果您想知道现有输入的索引,可以只包含一个三元组
var t1 = [0,0,0]; var t2 = [1,0,0]; var t3 = [0,0,1]; var userInput = [0,0,0]; var configs = [t1, t2, t3]; var inputIndex = -1; configs.some((a,n) => userInput.every((u,i) => a[i] == u)?(inputIndex=n,true):false); console.log(inputIndex);
Would this work? 这会有用吗?
var result = _.find(configs, function(t) {
return _.isEqual(userInput, t);
});
If you need to know the index: 如果您需要知道索引:
var index = -1;
_.some(configs, function(t, idx) {
if (_.isEqual(userInput, t)) {
index = idx;
return true;
}
});
// index = -1 if not found, else the first index of configs that matches the userInput
I would be concerned about the place where there might be duplicates. 我会担心可能存在重复的地方。 If you can make a function that can test equality reliably then this is just a filter.
如果你可以创建一个可以可靠地测试相等性的函数,那么这只是一个过滤器。
var matches = configs.filter(config => _.isEqual(config, userInput));
var match = matches[0];
If you wanted to invest into using a library more you could use something like find 如果你想投资使用更多的库,你可以使用像find这样的东西
var match = _.find(configs, config => _.isEqual(config, userInput));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.