在多维数组中搜索字符串

时间:2018-08-07 21:36:53

标签: javascript arrays

如果任何内部数组包含传递的字符串,如何在该数组的任何位置搜索?

var obj =[ [ [ 'hi' ], 'greeting' ],
  [ [], 'greeting' ],
  [ [ 'anyon' ], 'greeting' ],
  [ [ 'hello' ], 'greeting' ],
  [ [ 'good', 'day' ], 'greeting' ],
  [ [ 'bye' ], 'goodbye' ],
  [ [ 'lat' ], 'goodbye' ],
  [ [ 'goodby' ], 'goodbye' ],
  [ [ 'thank' ], 'thanks' ],
  [ [ 'thank' ], 'thanks' ],
  [ [ 'help' ], 'thanks' ],
  [ [ 'hour', 'op' ], 'hours' ],
  [ [ 'hour' ], 'hours' ],
  [ [ 'op' ], 'hours' ],
  [ [ 'mop' ], 'mopeds' ],
  [ [ 'kind', 'mop' ], 'mopeds' ],
  [ [ 'rent' ], 'mopeds' ],
  [ [ 'credit', 'card' ], 'payments' ],
  [ [ 'acceiv', 'mastercard' ], 'payments' ],
  [ [ 'cash' ] ],
  [ [ 'op', 'today' ], 'opentoday' ],
  [ [ 'op', 'today' ], 'opentoday' ],
  [ [ 'hour', 'today' ], 'opentoday' ],
  [ [ 'rent', 'mop' ], 'rental' ],
  [ [ 'rent', 'mop' ], 'rental' ],
  [ [ 'work' ], 'rental' ],
  [ [ 'today' ], 'today' ] ];

例如,如果进行搜索:

searchinarray('rental')

如果应返回true

2 个答案:

答案 0 :(得分:0)

您可以使用Array.prototype.reduceArray.prototype.concat递归地展平数组,然后检查新数组是否包含某个值。参见documentation

var obj =[ [ [ 'hi' ], 'greeting' ],
  [ [], 'greeting' ],
  [ [ 'anyon' ], 'greeting' ],
  [ [ 'hello' ], 'greeting' ],
  [ [ 'good', 'day' ], 'greeting' ],
  [ [ 'bye' ], 'goodbye' ],
  [ [ 'lat' ], 'goodbye' ],
  [ [ 'goodby' ], 'goodbye' ],
  [ [ 'thank' ], 'thanks' ],
  [ [ 'thank' ], 'thanks' ],
  [ [ 'help' ], 'thanks' ],
  [ [ 'hour', 'op' ], 'hours' ],
  [ [ 'hour' ], 'hours' ],
  [ [ 'op' ], 'hours' ],
  [ [ 'mop' ], 'mopeds' ],
  [ [ 'kind', 'mop' ], 'mopeds' ],
  [ [ 'rent' ], 'mopeds' ],
  [ [ 'credit', 'card' ], 'payments' ],
  [ [ 'acceiv', 'mastercard' ], 'payments' ],
  [ [ 'cash' ] ],
  [ [ 'op', 'today' ], 'opentoday' ],
  [ [ 'op', 'today' ], 'opentoday' ],
  [ [ 'hour', 'today' ], 'opentoday' ],
  [ [ 'rent', 'mop' ], 'rental' ],
  [ [ 'rent', 'mop' ], 'rental' ],
  [ [ 'work' ], 'rental' ],
  [ [ 'today' ], 'today' ] ];
function flattenDeep(arr1) {
   return arr1.reduce((acc, val) => Array.isArray(val) ? acc.concat(flattenDeep(val)) : acc.concat(val), []);
}
var obj2 = flattenDeep(obj);
console.log(obj2.includes('rental'));

答案 1 :(得分:0)

平整数组,然后搜索是解决此问题的一种方法,但是需要2个循环和更多分配才能在搜索数组之前对其进行转换。该函数仅循环遍历内部数组,如果字符串与您的toFind变量匹配,则返回真实结果。参见以下示例:

var obj =[ [ [ 'hi' ], 'greeting' ],
  [ [], 'greeting' ],
  [ [ 'anyon' ], 'greeting' ],
  [ [ 'hello' ], 'greeting' ],
  [ [ 'good', 'day' ], 'greeting' ],
  [ [ 'bye' ], 'goodbye' ],
  [ [ 'lat' ], 'goodbye' ],
  [ [ 'goodby' ], 'goodbye' ],
  [ [ 'thank' ], 'thanks' ],
  [ [ 'thank' ], 'thanks' ],
  [ [ 'help' ], 'thanks' ],
  [ [ 'hour', 'op' ], 'hours' ],
  [ [ 'hour' ], 'hours' ],
  [ [ 'op' ], 'hours' ],
  [ [ 'mop' ], 'mopeds' ],
  [ [ 'kind', 'mop' ], 'mopeds' ],
  [ [ 'rent' ], 'mopeds' ],
  [ [ 'credit', 'card' ], 'payments' ],
  [ [ 'acceiv', 'mastercard' ], 'payments' ],
  [ [ 'cash' ] ],
  [ [ 'op', 'today' ], 'opentoday' ],
  [ [ 'op', 'today' ], 'opentoday' ],
  [ [ 'hour', 'today' ], 'opentoday' ],
  [ [ 'rent', 'mop' ], 'rental' ],
  [ [ 'rent', 'mop' ], 'rental' ],
  [ [ 'work' ], 'rental' ],
  [ [ 'today' ], 'today' ] ];


function findRecursive(arr1, toFind) {
   return arr1.find(val => Array.isArray(val) ? findRecursive(val, toFind) : val === toFind) !== undefined;
}
console.log(findRecursive(obj, 'rental'));

相关问题