Tips for determining if an array has many occurrences of a string made up of separate whitespace values

Working with an Angular application that adds 'ng-star-inserted' to each node element. My task is to determine if the target element's class exists within an array.

https://i.sstatic.net/v8G97k6o.png

var footerElementClassList = [
      'footer-links__link',
      'social__link-icon',
      'bottom__link',
    ];

const filtered = footerElementClassList.includes('bottom__link ng-star-inserted');
console.log(filtered); 

Attempted the following options without success

Option 1

var footerElementClassList = [
      'footer-links__link',
      'social__link-icon',
      'bottom__link',
    ];
var mySet = new Set(footerElementClassList);
var hasB = mySet.has('footer-links__link ng-star-inserted');
console.log(hasB); //false

Option 2

var footerElementClassList = [
      'footer-links__link',
      'social__link-icon',
      'bottom__link',
    ];

function hasMatch(value) {
return value = 'bottom__link ng-star-inserted';
}
const filtered = footerElementClassList.filter(hasMatch);
console.log(filtered); //false

Option 3

var footerElementClassList = [
      'footer-links__link',
      'social__link-icon',
      'bottom__link',
    ];
console.log(footerElementClassList?.contains('footer-links__link ng-star-inserted'));

Option 4

var footerElementClassList = [
      'footer-links__link',
      'social__link-icon',
      'bottom__link',
    ];
const filtered = footerElementClassList.includes('bottom__link ng-star-inserted');
console.log(filtered); 

Answer №1

When dealing with data-formats, there are two key types that are commonly used:

  • Array-based list of class-names
  • Search query provided as a string of space-separated class-names

Due to the different formats, comparison approaches must be tailored accordingly. One approach involves comparing an array of string-based name values to a string of space-separated name-values, leading to mainly two possible methods:

  • Split the string of class-names and iterate through the resulting array to see if the class-list array includes every split class-name. This method has a quadratic complexity due to the nested includes within every.

    function hasEveryClassName(list, search) {
      return search
        .trim()
        .split(/\s+/)
        .every(value => list.includes(value));
    }
    
  • Create a RegExp from the split and sorted string of class-names which is then tested against the sorted and joined string version of the provided array of class-names. This approach involves sorting both participants and creating a valid regex on the fly.

    function hasEveryClassName(list, search) {
      return RegExp([
        '\\b',
        search
          .trim()
          .split(/\s+/)
          .sort()
          .join('\\b.*?\\b'),
        '\\b',
      ].join('')).test(list.sort().join(' '));
    }
    

Here is an example of the code in action:

const classList = [
  'footer-links__link',
  'social__link-icon',
  'bottom__link',
];
const classNames = 'bottom__link ng-star-inserted';

// Sample implementation of the methods
.as-console-wrapper { min-height: 100%!important; top: 0; }

Edit: In response to the OP's query about a simpler solution...

Is there any way much more simpler to achieve the solution?

Indeed, while the existing every/includes based solution is straightforward, replacing the array/list of class-names with a Set-based representation can offer better performance.

The original solution using every/includes can be modified as follows:

function hasEveryClassName(list, search) {
  const lookup = new Set(list);

  return search
    .trim()
    .split(/\s+/)
    .every(value => lookup.has(value));
}

Switching from an array to a set structure reduces the time complexity of the solution from quadratic to linear.

Here's an example of the updated code:

const classNameLookup = new Set([
  'footer-links__link',
  'social__link-icon',
  'bottom__link',
]);
const classNames = 'bottom__link ng-star-inserted';

function hasEveryClassName(lookup, search) {
  return search
    .trim()
    .split(/\s+/)
    .every(value => lookup.has(value));
}
.as-console-wrapper { min-height: 100%!important; top: 0; }

Answer №2

When providing classes as a single string separated by a space, the first step is to split them. This particular code snippet checks for the presence of all classes provided.

var footerElementClassList = [
      'footer-links__link',
      'social__link-icon',
      'bottom__link',
    ];

function containesAll(classString, classList){
  const classes = classString.split(' ');
 
  for(cls of classes)
  {
    if(!classList.includes(cls))
    {
      return false;
    }
  }
  return true;
}

console.log(`containesAll('bottom__link ng-star-inserted', footerElementClassList): ${containesAll('bottom__link ng-star-inserted', footerElementClassList)}`) // false
console.log(`containesAll('bottom__link social__link-icon', footerElementClassList): ${containesAll('bottom__link social__link-icon', footerElementClassList)}`) // true

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

Exploring the Power of Laravel 5.5 and Vue.js 2.x for Efficient API Calls

Currently, I am working on a Laravel 5.5 project locally that incorporates Vue.js 2.5.9 with XAMP Server. One of the tasks I have is to load certain information onto the DOM and then refresh it upon clicking the "Refresh" button. However, there seems to ...

What is the best way to turn off the legends in chart.js?

I'm having trouble customizing a chart using chart.js because I can't seem to disable the legends within the canvas element. However, I still want to style the legends elsewhere on the page using generateLegend(). Can anyone provide assistance wi ...

Issue with Materialize Sidenav: Not functional on iOS devices including iPhones, functions correctly on all other devices

My Materialize Sidenav is functioning on all devices except for iPad and iPhone. If you want to check out the code, here is the link to the repository: repo. Take a look at index.html (line 44 down) and js/onloadSetup.js. I attempted adding this in onload ...

Rendering data from an API using v-if

Could you help me change the tag that currently displays true or false? I want it to show "FREE" if the event is free and "PAID" if it's not. Check out the Eventbrite API here The response I'm receiving from data.events.is_free is in boolean fo ...

What is the best way to locate an item reference for a specific DOM element?

Imagine a vast DOM structure with approximately 10,000 HTML elements, including 1,000 span tags. None of the span elements have any identifiers and are buried deep within other objects with long, complex Xpath paths that are not user-friendly for selectio ...

Prevent the selection of rows and only enable the option to select checkboxes in PrimeNG's Multiselect feature

I'm currently working on a task that involves using PrimeNG multiselect. This particular multiselect includes checkboxes followed by text within each row. I need to disable the ability to select rows by clicking on the text, and instead only allow sel ...

The $scope variable does not sync with the express API

I have noticed that the static part of my app is loading fine. Recently, I integrated a service using Express as a simple API. However, when I attempt to set the #scope from my module's controller, it seems like it hasn't loaded at all. I am puzz ...

Trigger a page refresh when you revisit it

Currently, I am utilizing Nuxt in SPA mode and my page structure is set up like this: pages ... - users/ - - index - - new - - update/ - - - _id ... I have a page dedicated to displaying a list of users along with a 'subpage' for adding new u ...

Error: Invariant violation - App component did not return anything in the render method

I am facing an issue while attempting to render a component based on the promise returned from AsyncStorage. The error message I receive is: Error: Invariant Violation: App(...): No content was returned from the render method. This typically indicates ...

Looping through multi-dimensional JSON objects with jQuery

Hello there, I'm currently facing some challenges in getting the screen below to work properly: Project progress screen I have generated the following JSON data from PHP and MYSQL. My goal is to display the project alongside user images and names whe ...

How can we avoid excessive re-rendering of a child component in React when making changes to the parent's state?

In my React application, I am facing a situation where a parent component controls a state variable and sends it to a child component. The child component utilizes this state in its useEffect hook and at times modifies the parent's state. As a result, ...

Incorporate the jquery lazy load plugin alongside ZURB foundation data-interchange for optimal performance

Currently, I am engaged in a project that involves utilizing the ZURB foundation framework alongside its data-interchange feature to display various images based on different screen sizes. To learn more about this method, please visit: You can also explo ...

Finding differences between two 24-hour format times using moment.js

Is there a way to compare two times in 24-hour format using the code below? $("#dd_start_timing, #dd_end_timing").on('keyup change keydown', function() { var DutyDayStartTime = $("#dd_start_timing").val().trim();// 13:05 var ...

Shopify module is throwing an error stating that React is not defined

I wanted to create my first Shopify module, but I encountered an error in my application page on the shop while using React. https://i.sstatic.net/Q02yM.png Here is my index.js code: import {Page} from "@shopify/polaris"; import {ResourcePicker ...

Is it possible to use JavaScript to click on a particular point or element within a canvas?

Is there a way to trigger a click at a specific point on a canvas without direct access to the code that generates it? I've attempted using coordinates, but haven't had any success. Any alternative suggestions would be appreciated. UPDATE: To pr ...

Calculate the total price using jQuery

I’m a beginner in JavaScript and I’ve hit a roadblock. I have a plus and minus button that adds up product quantities, but I need the total price to reflect this as well. Some products can only be purchased in multiples of 2, 5 or 10. Here is the HTML ...

Using ReactJS and JavaScript to transform an array into a fresh array

I am working with an array that looks like this: var oldArray = [ {number: '12345', alphabet: 'abcde'}, {number: '54321', alphabet: 'abcde'}, {number: '67891', alphabet: 'abcde'}, ...

Using jQuery AJAX, the value of a server-side control (textbox) can be easily set

After working with the code below, I noticed that I can only set the value received from an ajax call if I am using HTML controls like buttons and text boxes. If I try to use asp server controls such as a button, the ajax call does not return any output, e ...

Error: The validation of a JSON request failed as schema.validate is not a recognized function

As a beginner, I am currently immersed in a node.js API authentication tutorial. Everything was going smoothly until I had to refactor my code into separate files. Now, every time I send a JSON request via Postman, I keep encountering the error message "Ty ...

Is there a way to upload a kml file to Google Maps using my website or by using JavaScript commands?

I have information on my website regarding gas stations, including the quality of gasoline and the GPS coordinates of each station. My concept involves incorporating Google Maps into my site, similar to how flightradar24.com displays pins indicating gas s ...