Traversing a multidimensional array with jQuery

In my quest to iterate through a multidimensional array, I encountered the need to remove the class "list" from the divs within the array. However, there could be multiple divs with this class on my site.

My initial approach involved using two for-loops, but unfortunately, it was unsuccessful.

const $lists = [
                $('.first'),
                $('.third')
            ];

var i, j;
for (i = 0; i < $lists.length; i++) {
    for (j = 0; j < $lists[i].length; j++){
        $lists[i][j].find(.list).removeClass(list);
    }
}
.list {
  background: green;
}
<div class="first">
  <ul class="list">
    <li>foo</li>
    <li>fooooo</li>
    <li>bar</li>
  </ul>
</div>

<div class="first">
  <ul class="list">
    <li>foo</li>
    <li>fooooo</li>
    <li>bar</li>
  </ul>
</div>

<div class="second">
   <ul class="list">
    <li>foo</li>
    <li>fooooo</li>
    <li>bar</li>
  </ul>
</div>

<div class="third">
   <ul class="list">
    <li>foo</li>
    <li>fooooo</li>
    <li>bar</li>
  </ul>
</div>

Exploring the possibility of using CSS and :not[], but unfortunately that approach did not yield the desired outcome as well.

.

 .list:not(.first)  {
     border: 3px solid red; }

Answer №1

Blend the chosen divs together using jQuery.merge(), locate all elements with the .list class, and delete it:

If you only want to delete it from the first and third:

var $lists = [
    $('.first'),
    $('.third')
];

$($.merge.apply([], $lists)).find('.list').removeClass('list');
.list {
  background: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="first">
  <ul class="list">
    <li>foo</li>
    <li>fooooo</li>
    <li>bar</li>
  </ul>
</div>

<div class="second">
   <ul class="list">
    <li>foo</li>
    <li>fooooo</li>
    <li>bar</li>
  </ul>
</div>

<div class="third">
   <ul class="list">
    <li>foo</li>
    <li>fooooo</li>
    <li>bar</li>
  </ul>
</div>

Answer №2

$(document).ready(function(){
$(".second").not(".first, .third").addClass("list")
});
.list {
  background: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="first">
  <ul>
    <li>alpha</li>
    <li>bravo</li>
    <li>charlie</li>
  </ul>
</div>

<div class="second">
   <ul>
    <li>alpha</li>
    <li>bravo</li>
    <li>charlie</li>
  </ul>
</div>

<div class="third">
   <ul>
    <li>alpha</li>
    <li>bravo</li>
    <li>charlie</li>
  </ul>
</div>

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

JavaScript Tutorial: Updating the maximum value of a gauge chart

Is there a way to dynamically change both the total and max values in my current code? The maximum value needs to be dynamic based on the filter applied. Here is the code snippet: resource1=[ "//cdn.jsdelivr.net/raphael/2.1.0/raphael-min.js", "//cd ...

A Sweet Alert to Deliver your Morning Toasty Message

I seem to be encountering an issue with my toast message. Instead of the toast popping up immediately after submitting the form, it keeps appearing even if I haven't submitted the form and even when navigating from another page to this one, the toast ...

How come I keep running into the "is not a function" issue when trying to use the generateRequest function with Polymer's iron-ajax

Oops, it seems like there was an error: Uncaught TypeError: this.$.ajax.generateRequest is not a function. The issue seems to be in assets-ajax.html at line 23. <dom-module id="assets-pull"> <style> </style> <template> <but ...

Updating Angular UI-Router to version 1.0 causes issues with resolving data inside views

After upgrading my Angular UI-Router to version 1.0, I came across an interesting statement in the migration guide: We no longer process resolve blocks that are declared inside a views While it makes sense to move all resolve blocks to the parent state ...

Guide to building a JavaScript array and storing data from a separate array into the newly created array in JavaScript

Can you help me create a JavaScript array and save another array of data to be part of the newly created array in JavaScript? I attempted it using the code below. Code: var vvv=this.new_products.length-this.quote.lines.length; let mmm={}; if(v ...

Updating a select menu with AJAX without the need for a <div> tag within the form

Utilizing an ajax call to dynamically update a select menu is a common practice. The ajax call fetches the options from a separate .php file and populates the select menu accordingly. Below is the code snippet for inserting the dynamic content using JavaS ...

Fetch a document from a NodeJS Server utilizing Express

Is there a way to download a file from my server to my machine by accessing a page on a nodeJS server? I am currently using ExpressJS and I have attempted the following: app.get('/download', function(req, res){ var file = fs.readFileSync(__d ...

Experiencing difficulty importing .pem files into Express

Referred to by this previous inquiry which went unanswered: I've stumbled upon something peculiar. Despite my efforts, I am still unable to import my certificate. Every time I attempt to run my Node/express application, it crashes with the same error ...

Differences in results occur when JavaScript function declarations are used in both Native browser and Node.js environments

When running the code below, the results differ between Browser and Node.js environments. In the browser, the result is a. In Node.js, the result is b. if (1) { function foo() { return 'a'; } } else { function foo() { return 'b&ap ...

Sending arguments to Angular UI router template

My angular ui router is changing states/views as: $stateProvider .state({ name: 'home', url: '/', template: '<home-view></home-view>', }) Is it possible to ...

The file uploader on the HTML page only allows for PNG files to be uploaded

Currently, I am working on an application where I am facing a challenge related to file uploads. Specifically, I have an input field like this: <input id="full_demo" type="hidden" name="test[image]"> I am looking for a way to restrict the upload of ...

React form events onSubmit and onClick fail to trigger within Zurb Foundation's 'Reveal' modal dialog

Since upgrading from React 16.12.0 to React 17.0.0, I've encountered an issue with my code. Previously, everything worked perfectly fine, but now none of my events seem to be firing (meaning the console.log statement never appears). class MyClass exte ...

Ways to develop tests for functions specific to my scenario

Currently, I am attempting to implement a timeout function test within my application. Within the controller: $scope.$watch('toy',function(toyVar){ if(toyVar == 1) { //perform actions } else { $timeout(f ...

What are the steps for implementing pytesseract on a node.js server?

While working on my node.js server, I encountered an issue when using child-process to send an image to a python script. Although I can successfully read the image in the python script, I encounter an error when attempting to convert it to text using pytes ...

AngularJS Error: $interpolate:interr - Encounter with Interpolation Error

Having some trouble embedding a YouTube video into my website using AngularJS. Keep receiving this pesky error: Error: $interpolate:interr Interpolation Error Any idea why this error is popping up and how I can resolve it? Just trying to add the video... ...

What could be causing my vm root to be defined as app.__vue__?

I am facing an issue where my root vm in Vue is being set to app.__vue__ instead of just app. For instance, when trying to access the router, I have to use app.__vue__.$router. This seems unnecessary and confusing. There's more code in the script, bu ...

Having trouble displaying dynamically added images in my jsx-react component. The images are not showing up as expected

import React from "react"; import ReactDOM from "react-dom"; var bImg = prompt("Enter the URL of the image you want to set as the background:"); const bStyle = { backgroundImage: "url(bImg)"; // The link is stored ...

The attribute 'nameFormControl' is not a valid property on the 'ContactComponent' component

Greetings, StackOverflow community! I'm currently working on an Angular + Node app and I've run into a major problem related to forms and validators in Material Angular. Right now, I'm facing an issue where the HTML component is not recogn ...

Displaying an undefined value using ExpressJs (to present an [object])

While learning expressjs, I encountered an issue where the value I passed did not appear in the output. I created a new Routes file and defined a function with Route methods in it. However, when I set Route paths, the value did not show up in the output. A ...

Overlapping Divs and Colliding Elements

I am currently exploring the moment when my two yellow divs will overlap and make contact with each other. It seems that the collision detection is triggering true even when the divs are not intersecting. If you'd like to take a look at the example, ...