Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

136
Views
Cannot search in 2d array with includes()

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 ;
    }
})`
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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>

about 4 years ago · Juan Pablo Isaza Report

0

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))

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!