JavaScript:遍历多维数组并提取多个搜索值
我想做什么:JavaScript:遍历多维数组并提取多个搜索值,javascript,arrays,multidimensional-array,Javascript,Arrays,Multidimensional Array,我想做什么: var multiArray = [['Apples and pears', 'tuna salad'],['bananas tuna'],['the colour blue']]; var singleArray = []; var match_array = []; // Turn multiple arrays into one big one for (var i = 0; i < multiArray.length; i++) { f
var multiArray = [['Apples and pears', 'tuna salad'],['bananas tuna'],['the colour blue']];
var singleArray = [];
var match_array = [];
// Turn multiple arrays into one big one
for (var i = 0; i < multiArray.length; i++) {
for (var x = 0; x < multiArray[i].length; x++) {
singleArray.push(multiArray[i][x]);
}
}
// Create a new array from matched strings
function find_match(to_match) {
for (var i in singleArray) {
if (singleArray[i].indexOf(to_match)!= -1)
match_array.push(singleArray[i]);
}
return (match_array.length === 0 ? null : match_array);
}
// Find matching terms for match_array
find_match('tuna');
find_match('the');
alert(match_array);
使用多个搜索字符串在多维数组中搜索
我目前正在做的事情的示例:
var multiArray = [['Apples and pears', 'tuna salad'],['bananas tuna'],['the colour blue']];
var singleArray = [];
var match_array = [];
// Turn multiple arrays into one big one
for (var i = 0; i < multiArray.length; i++) {
for (var x = 0; x < multiArray[i].length; x++) {
singleArray.push(multiArray[i][x]);
}
}
// Create a new array from matched strings
function find_match(to_match) {
for (var i in singleArray) {
if (singleArray[i].indexOf(to_match)!= -1)
match_array.push(singleArray[i]);
}
return (match_array.length === 0 ? null : match_array);
}
// Find matching terms for match_array
find_match('tuna');
find_match('the');
alert(match_array);
var multiArray=[['苹果和梨','金枪鱼沙拉',['香蕉金枪鱼'],['蓝色'];
var singleArray=[];
var match_数组=[];
//将多个阵列转换为一个大阵列
对于(var i=0;i
问题:
var multiArray = [['Apples and pears', 'tuna salad'],['bananas tuna'],['the colour blue']];
var singleArray = [];
var match_array = [];
// Turn multiple arrays into one big one
for (var i = 0; i < multiArray.length; i++) {
for (var x = 0; x < multiArray[i].length; x++) {
singleArray.push(multiArray[i][x]);
}
}
// Create a new array from matched strings
function find_match(to_match) {
for (var i in singleArray) {
if (singleArray[i].indexOf(to_match)!= -1)
match_array.push(singleArray[i]);
}
return (match_array.length === 0 ? null : match_array);
}
// Find matching terms for match_array
find_match('tuna');
find_match('the');
alert(match_array);
多数组
,不使用
多个find_match
函数)你想要这样的吗
var multiArray = [['Foo', 'Bar'],['Foo'],['Bar']];
var singleArray = [];
var match_array = [];
// Turn multiple arrays into one big one
for (var i = 0; i < multiArray.length; i++) {
for (var x = 0; x < multiArray[i].length; x++) {
singleArray.push(multiArray[i][x]);
}
}
// Create a new array from matched strings
function find_match(to_match, where_search) {
for (var j in where_search) {
if(where_search[j] instanceof Array){
find_match(to_match, where_search[j]);
}else{
for (var i in to_match) {
if (where_search[j].indexOf(to_match[i]) ==0 &&
where_search[j].length == to_match[i].length)
match_array.push(where_search[j]);
}
}
}
return (where_search.length === 0 ? null : match_array);
}
// Find matching terms for match_array
find_match(['Foo', 'Bar'],multiArray);
alert(match_array);
var multiArray=[['Foo',Bar'],['Foo'],['Bar'];
var singleArray=[];
var match_数组=[];
//将多个阵列转换为一个大阵列
对于(var i=0;i
当我通过JSFIDLE运行这个脚本时,它只会提醒“金枪鱼沙拉”。不完全是。。。现在它提醒“金枪鱼沙拉”,“香蕉金枪鱼”您需要在多数组中匹配整个数组搜索吗?示例:multi=['Foo','Bar'],['Foo'],['Bar']]搜索['Foo','Bar']-返回['Foo','Bar']?理想情况下,使用您的示例,搜索['Foo','Bar']
返回['Foo','Bar','Foo','Bar']
立即尝试。如何简化此过程(即直接搜索多数组,而不使用多个查找匹配函数)?您可以向数组中添加新函数。示例:Array.prototype.add=function(foo){this.push(foo)};p=[];p、 添加(“a”);p、 添加(“b”);p、 添加(“c”);警报(p)