Search for nested arrays in javascript

Hello. I am puzzling over the task of searching in an array containing an unknown number of nested arrays. The function should just show whether there is such an element or not, takes two arguments: the actual array and the desired element. But in the process, I came to a dead end. I would be grateful for any hints on this issue. Thanks.

function deepSearch (array, item) {
      var len = array.length;
         for(var i=0; i<len; i++){
        if(array[i] == Array){
          if(array[i].deepSearch(array, item)){
        return true;
        break;
      }
        else {
        if(array[i]==item){
          return true;
          break;
        }
      }
    }
  }
  return false;
}

Application example:

console.log(deepSearch([1, 2, [3, 7, [22, [23]], 4, 5], 17], 7));
Author: K.Miro, 2016-03-11

2 answers

One of the options using the function some

function deepSearch(array, item) {
  return array.some(function s(el) {
    return el == item || ((el instanceof Array) && el.some(s));
  })
}

document.write("array: [1, 2, [3, 7, [22, [23]], 4, 5], 17], item: 22, search: " + deepSearch([1, 2, [3, 7, [22, [23]], 4, 5], 17], 22));

You should pay more attention to the formatting of the code. In addition to the two errors with the definition of array element or not and the function call for the element, there is also a problem with the brackets around if.

function deepSearch(array, item) {
  console.log(array, item);
  var len = array.length;
  for (var i = 0; i < len; i++) {
    if (array[i] instanceof Array) { // array[i] == Array
      if (deepSearch(array[i], item)) { // было array[i].deepSearch(array, item)
        return true;
      }
    } else {
      if (array[i] == item) {
        return true;
      }
    }
  }
  return false;
}

document.write("array: [1, 2, [3, 7, [22, [23]], 4, 5], 17], item: 7, search: " + deepSearch([1, 2, [3, 7, [22, [23]], 4, 5], 17], 7));
 3
Author: Grundy, 2016-03-11 09:45:50

There are several errors:

  1. Incorrectly determining whether the array is caught. Modern JS has a method Array.isArray(x), checking whether its argument is an array.

  2. If you are already returning a result from the f-ai, then nothing will be executed after returnbreak is not needed.

  3. deepSearch() in your code, there is no way it can be a method of the array[i] object – so you can't call it that way: array[i].deepSearch()

Algorithm after all pretty simple:

  1. At the input, we get an array and consider it as one-dimensional, iterating through the elements in turn.
  2. If the element is equal to the desired one-all, the output is with true.
  3. If we get an array, we call the same function and pass the array element there as a parameter. If this call returns true, it is the same as if you just found what you were looking for in the. All element.

function deepSearch(arr, item) {
  for (var i = 0; i < arr.length; i++) {
    if (Array.isArray(arr[i]) && deepSearch(arr[i], item)) {
      return true;
    } else if (arr[i] === item) {
      return true;
    } else {
      // ищем дальше
    }
  }
  return false;
}

document.body.innerHTML += deepSearch([1, 2, [3, 7, [22, [23]], 4, 5], 17], 7).toString() + ' ';
document.body.innerHTML += deepSearch([1, 2, [3, 7, [22, [23]], 4, 5], 17], 9).toString();

You can reduce the code to an unreadable two lines:

function deepSearch(arr, item) {
    for ( var i=0; i < arr.length; i++) if( arr[i]===item || (Array.isArray(arr[i])  &&  deepSearch(arr[i], item))) return true;
    return false;
}
 2
Author: Sergiks, 2016-03-11 10:03:34