Seemingly empty Array after push

597 views Asked by At

I'm just trying to search through a two dimensional array of strings.

The results in found are correct, but the row isn't pushed (correctly) into the array.

The browser shows me this:

enter image description here

function filterItems(arr, query) {
  return arr.filter(function(el) {
    return el.toLowerCase().includes(query.toLowerCase());
  });
}

function FilterArray(unfiltered_array, searchphrase) {
  var filtered_array = [];
  for (var i = 0; i < unfiltered_array.length; i++) {
    var row = unfiltered_array[i];
    var found = filterItems(row, searchphrase);

    if (found.length) {
      console.log(row);
      filtered_array.push(row);
    }
  }


  return filtered_array;

}

const filtered = FilterArray([
  ["LC53005", "bob", "lc56778"],
  ["FP53001", "john", "dog"]
], "lc5");

console.log({
  filtered
});

and I was searching for LC53005.

Any ideas?

1

There are 1 answers

1
Tushar Wasson On

Clearer approach with the expected result !

var unfiltered_array = [
  ["LC53005", "bob", "cat"],
  ["FP53001", "john", "dog"]
];

function filterItems(arr, query) {

  return arr.filter(function(el) {
    return (el.toLowerCase().includes(query.toLowerCase()));
  })
}

function FilterArrayMap(unfiltered_array, searchphrase) {
  return unfiltered_array.map(k => {
    return filterItems(k, searchphrase);
  }).flat();
}

console.log(FilterArrayMap(unfiltered_array, "LC53005"));