Home > OS >  Cannot search in 2d array with includes()
Cannot search in 2d array with includes()

Time:03-23

I am trying to get data from 2d array with include function but it doesn't work only search method is running here is my Code

    let arr = [ ['Ahmed','Engineer','24'] ,
            ['Seif Ossama Ahmed','Student','21'],
            ['Hassan','Doctor','27'],
];
let outputText = "" ;
let submitBtn = document.getElementById('submit-btn');
let output = document.getElementById('output-here');
 
submitBtn.addEventListener('click',function(){
    outputText = "";
    let q = document.getElementById('search-bar');
    for(let i = 0; i<arr.length; i  ){
        for(let j = 0; j<arr[i].length;j  ){
            if(arr[i][j].includes(q.value) != -1){
                outputText  = arr[i] ;
                break;
            }else if(q.value === ""){
                outputText = "Sorry No result" ;
            }
        }
        output.innerHTML = outputText ;
    }
})`

CodePudding user response:

arr[i][j].includes(q.value) != -1

this would be your first problem as not even false == -1. false is equal to 0.

just write if (arr[i][j].includes(q.value))

CodePudding user response:

You can use some

The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn't modify the array.

const arr = [
  ['Ahmed', 'Engineer', '24'],
  ['Seif Ossama Ahmed', 'Student', '21'],
  ['Hassan', 'Doctor', '27']
];

const submitBtn = document.getElementById('submit-btn')

function search(arr, inputText) {
  console.log(arr.some(person => person.includes(inputText)))
}

submitBtn.addEventListener('click', () => search(arr, 'Ahmed'))
<button id="submit-btn"> Submit </button>

CodePudding user response:

As pointed out by others includes() returns a boolean.

Here your example with the search that is case-insensitive and returns all results found.

let arr = [
  ["Ahmed", "Engineer", "24"],
  ["Seif Ossama Ahmed", "Student", "21"],
  ["Hassan", "Doctor", "27"],
];

window.addEventListener("DOMContentLoaded", (event) => {
  const submitBtn = document.getElementById("submit-btn");
  const output = document.getElementById("output-here");
  

  submitBtn.addEventListener("click", function () {
    const q = document.getElementById("search-bar");
    // search
    const results = findAllCaseInsensitive(q.value);
    
    // display results
    let outputText = "";
    if (results.length === 0) outputText = "Sorry No result";
    else outputText = results.join(" || ");
    output.textContent = outputText;
  });
});

/**
 * Search for a string (case-insensitive) and return ALL occurrences. 
 * @param {string} value value to search for 
 * @returns 
 */
function findAllCaseInsensitive(value) {
  value = value.toUpperCase();
  const results = [];
  for (let i = 0; i < arr.length; i  ) {
    for (let j = 0; j < arr[i].length; j  ) {
      if (arr[i][j].toUpperCase().includes(value)) {
        results.push(arr[i]);
      }
    }
  }
  return results;
}
<input id="search-bar"></input>
<button id="submit-btn">Search</button>
<span id="output-here"></span>

  • Related