Searching for a specific sequence of characters within an array

I have been working on a versatile function that can perform various tasks related to finding a specific value in a list or array. Below is the code I have come up with:

function findInArray(needle, arr, exact, sensitive) {
  if (needle && arr) {
    var hayLength = arr.length
    for (var i = 0; i < hayLength; i++) {
      if (arr[0].length >= 0) {var haystack = arr[i][0];}
      else {haystack = arr[i];}
      if (exact && sensitive && (haystack === needle)) {return i;}
      else if (exact && !(sensitive) && (haystack == needle)) {return i;}
      else if (!(exact) && sensitive && (haystack.toLowerCase().search(needle.toLowerCase()))>-1) {return i;}
      else if (!(exact) && !(sensitive) && haystack.search(needle)>-1) {return i;}
    }
  }
  return -1;
}

Although I believe the code can be optimized further, I am facing an issue with the third condition when attempting to ignore case sensitivity to match a string in a list. For example:

var arr = ["Partner1", "Partner2"]
var needle = "partner1"
var n = findInArray(needle, arr, true, false);

It returns -1.

I aim for this function to be able to work with both 1D and multidimensional lists and to find substrings (e.g. match "Google" and "Googler").

Answered: Drawing inspiration from responses by @NoobishPro and @tehhowch, the following optimized code resolves the sensitivity parameter efficiently:

function findInArray(needle, arr, exact, sensitive) {
  exact = exact !== false;
  sensitive = sensitive !== false;

  //Accounting for sensitivity parameter to enhance performance
  if (!sensitive) {
    needle = needle.toLowerCase();
  }

  //Determining array length
  var hayLength = arr.length;
  for (var i = 0; i < hayLength; i++) {
    //Setting haystack
    var haystack = arr[i];
    //Checking for nested arrays and handling them recursively
    if (haystack.constructor == Array) {
      return findInArray(needle, haystack, exact, sensitive);
    }

    //Performing additional lowercasing if sensitivity is disabled
    if (!sensitive) {
      haystack = haystack.toLowerCase();
    }

    //Handling different scenarios based on exact and sensitivity settings
    if (exact && sensitive && (haystack == needle)) {
      return i;
    } else if (exact & (haystack == needle)) {
      return i;
    } else if (!exact & (haystack.search(needle)) > -1) {
      return i;
    }
  }
  return -1;
}

Answer №1

LINK TO FUNCTIONAL JSFIDDLE

The reason for the issue with your recursion attempt was due to some errors in the code structure. Despite that, most of your code was well-written. Additionally, you overlooked implementing the toLowerCase() method in one instance.

The corrected code is as follows;

var arr = ["Partner1", "Partner2"]
var needle = "partner1"
var n = findInArray(needle, arr, true, false);
console.log(n);

function findInArray(needle, arr, exact, sensitive) {
  //Check if these attributes were even provided
  if (typeof needle != 'undefined' && typeof arr != 'undefined') {
    if (arr.length < 1) {
      return -1;
    }
    if (typeof exact == 'undefined') {
      //Ensuring it's always set. Defaults to false.
      exact = false;
    }
    if (sensitive == 'undefined') {
      //Ensuring it's always set. Defaults to false.
      sensitive = false;
    }
    //determining array length
    var hayLength = arr.length;
    for (var i = 0; i < hayLength; i++) {
      //Set haystack
      var haystack = arr[i];
      //Check if it's another array. If so, recursively call this function to go 1 level deeper.
      if (haystack.constructor == Array) {
        return findInArray(needle, haystack, exact, sensitive);
      }

      //Correction made to implement toLowerCase method on the last comparison
      if (exact && sensitive && (haystack === needle)) {
        return i;
      } else if (exact && !(sensitive) && (haystack.toLowerCase() == needle.toLowerCase())) {
        return i;
      } else if (!(exact) && sensitive && (haystack.search(needle)) > -1) {
        return i;
      } else if (!(exact) && !(sensitive) && haystack.toLowerCase().search(needle.toLowerCase()) > -1) {
        return i;
      }
    }
  }
  return -1;
}

Minor optimizations

I have also incorporated a few refinements to enhance the efficiency of the code. LINK TO OPTIMIZED JSFIDDLE

var arr = ["Partner1", "Partner2"]
var needle = "partner2"
var n = findInArray(needle, arr, true, false);
console.log(n);

function findInArray(needle, arr, exact, sensitive) {
  //Check if these attributes were even provided
  if (typeof needle != 'undefined' && typeof arr != 'undefined') {
    if (arr.length < 1) {
      return -1;
    }
    if (typeof exact == 'undefined') {
      //Ensuring it's always set. Defaults to false.
      exact = false;
    }
    if (sensitive == 'undefined') {
      //Ensuring it's always set. Defaults to false.
      sensitive = false;
    }

    //Handle sensitivity parameter for improved performance
    if (!sensitive) {
      needle = needle.toLowerCase();
    }

    //determine array length
    var hayLength = arr.length;
    for (var i = 0; i < hayLength; i++) {
      //Set haystack
      var haystack = arr[i];
      //Check if it's another array. If so, recursively call this function to go 1 level deeper.
      if (haystack.constructor == Array) {
        return findInArray(needle, haystack, exact, sensitive);
      }

      //Implementing a lowercase operation here to save on lowercase condition checks
      if (!sensitive) {
        haystack = haystack.toLowerCase();
      }

      //Simplified conditionals
      if (exact && sensitive && (haystack == needle)) {
        return i;
      } else if (exact & (haystack == needle)) {
        return i;
      } else if (!exact & (haystack.search(needle)) > -1) {
        return i;
      }
    }
  }
  return -1;
}

Answer №2

You have the option of utilizing the built-in Array.prototype.find or Array.prototype.indexOf

var arr = ["Partner1", "Partner2"];
var needle = "partner1";
var n = findInArray(needle, arr, true, false);
console.log(n);
function findInArray (input, array, exact, caseSenstive) {
   if (caseSenstive) {
      input = input.toLowerCase();
   }
   if (!exact){
      return array.find(i => i.toLowerCase().indexOf(input)) || -1;
   }
   if (caseSenstive) {
      return array.find(i => i === input) || -1;
   }
   return array.find(i => i.toLowerCase() === input) || -1;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using jQuery to display items from GitHub API in a custom unordered list format

Attempting to access data from the GitHub API using jQuery (AJAX) and display it on a static webpage. Here are the HTML and JS code snippets: $(document).ready(function(){ $.ajax({ url: 'https://api.github.com/re ...

What is the best practice for naming variables in JavaScript?

Currently, my API is built using PHP Laravel and MySQL, which uses snake_case for field names. I am considering using the same naming convention in client-side JavaScript to make it easier to transfer field names from PHP code to JavaScript code and when m ...

I encounter difficulties using my static resources in the root route of my Express.js application

Can you guide me on how to implement styles and images from the assets folder in my webpage, specifically for the root route? As an example, I have a styles.css file located at assets/styles.css. In my code, I am using app.use(express.static('assets&a ...

Oops! Looks like there was an error. The text strings need to be displayed within a <Text> component in the BottomTabBar. This occurred at line 132 in the

My app includes a bottomTab Navigation feature, but I encountered an error: Error: Text strings must be rendered within a <Text> component. This error is located at: in BottomTabBar (at SceneView.tsx:132) in StaticContainer in StaticCont ...

The Null object within localStorage is identified as a String data type

As someone transitioning from Java development to Javascript, I am seeking clarification on a particular issue. In my project, I am utilizing localStorage to keep track of the user's token in the browser. localStorage.token = 'xxx' When a ...

VueJS method for making an HTTP GET request

Attempting to make an http get request using Vue js. I can't seem to find any issues with the logic, although I'm not very experienced with vuejs. Continuously encountering these two errors: [Vue warn]: Error in mounted hook: "TypeError: Cann ...

Display a div based on user selection using Ajax and Laravel

There are two div elements on the index page containing a datatable. By default, these two divs should be hidden. When an option is selected from the dropdown menu, the corresponding div should be displayed. The webpage is designed for searching within a ...

How can I transform area, city, state, and country into latitude and longitude using Google Maps API v3?

Is there a way to retrieve the latitude and longitude for a string that includes area name, city name, state name, and country name using Google Maps API V3? ...

Error encountered: Mocha - The property '$scope' cannot be read as it is undefined

I encountered an issue: Error: Type Error - Cannot read property '$scope' of undefined at $controller (angular/angular.js:10327:28) at angular-mocks/angular-mocks.js:2221:12 at Context. (src/client/app/peer-review/post-visit.co ...

Is there a way to trigger an Ajax function after individually selecting each checkbox in a list in MVC 4 using Razor syntax?

Is it possible to trigger an AJAX function when a checkbox within the ul below is checked? Each checkbox has a unique name attribute, such as chkbrand_1, chkbrand_2, chkbrand_3, etc. I am able to obtain the correct value using this code: $('.sear ...

Encountering issues with webpack module federation and single-spa-vue integration, specifically with Vue version 2.16.12

I'm currently facing an issue while developing a microfrontend using single-spa-vue and Vue 2.6.12. To set up my project, I am utilizing the webpack module federation plugin. Below is the entry point for my application: src/app.ts import singleSpaV ...

Converting a JavaScript variable into an xls or csv file

My application currently uses Javascript for calculations and data plotting, but I want to give users the ability to download the data as a csv or xls file. Is there a way in Javascript or another method where users can click a button, enter a filename, an ...

Combine TypeScript files in a specific sequence following compilation

I am hoping to utilize gulp for the following tasks: Compiling TypeScript to JavaScript, which is easily achievable Concatenating JavaScript files in a specific order, proving to be challenging Since I am developing an Angular application, it is crucial ...

Creating a new object store in IndexedDB on Windows 8

Encountering issues with creating an object store in IndexedDb while trying to build a Metro app using Javascript. The code snippet below shows my attempt within the 'dbReq.onsuccess' function that is supposed to create the object store upon succ ...

establishing the dimensions of table data cells

I'm facing a challenge with setting the width and height for table data that changes dynamically based on different values. The dimensions of the table itself are not definite, so I need to find a solution. Here's the code snippet I'm curren ...

Angular: Defining variables using let and var

When working with TypeScript and JavaScript, we typically use either let or var to declare a variable. However, in Angular components, we do not use them even though Angular itself uses TypeScript. For instance, export class ProductComponent implements OnI ...

Issue with reflect metadata in Next.js edge runtime causing functional problems

Currently, I am utilizing a package in my upcoming 13 app that incorporates reflect metadata. Unfortunately, during the next build process, an error occurs for which I haven't been able to find a solution. ../../eshop-sdk-js/node_modules/reflect-metad ...

Angular 2 partial static routing parameters with customizable features

Can an angular 2 routing configuration include a partial-static parameter? Currently, I am using a classic parameter setup like this: const routes: Routes = [ { path: ':type/fine.html', pathMatch: 'full', redirectTo: &ap ...

Javascript code not running as expected

Check out this code snippet: function generateRandomTeams() { const prom = new Promise(() => { // ... console.log('teams', props.state.teams) // logs }) .then(() => { console.log('here') // doesn't log }) ...

Toggling javascript functionality based on media queries

On one of my slides, I want to display images that are specific to different devices like iPhone, iPad, and desktop. I am looking for a way to filter these images based on the device. Using display:none won't work as it's JavaScript, but here is ...