Using jQuery and JSON: selecting classes based on associated JSON strings

If you want to see the complete codepen, I will share it here: http://codepen.io/JTBennett/pen/NdLWJy

Let me try to simplify the problem. I have a click event that selects from a JSON object based on one of its string values. Text from various strings is placed inside divs, but I want some hidden divs to fade in if a specific value is found within a group of strings in the JSON object (I used identical class names to make it easier).

Here's an illustration of one of the JSON objects:

{
"CSS":"A_Aeolian",
"root":"A",
"scale":"Aeolian",
"name":"A Aeolian",
"type":"Scale",
"structure":"A B C D E F G",
"noteClass_1":"A",
"noteClass_2":"B",
"noteClass_3":"C",
"noteClass_4":"D",
"noteClass_5":"E",
"noteClass_6":"F",
"noteClass_7":"G",
"noteClass_8":""
}

And this is my jQuery code:

$('.A, .As, .B, .C, .Cs, .D, .Ds, .E, .F, .Fs, .G, .Gs').hide();

$.getJSON("https://api.myjson.com/bins/non2t.json", function(data) {
  $('.main-btn').on("click", function () {
    $('#total-scale-men').empty()
    console.log("emptied total-scale-men");
    $('.gcMen ul li button').css({'border' : '1px #666 solid',
   'color' : '#666'})
    $('.scales-men').hide()
    $('.scales-men').fadeIn()
    $(this).css({'border' : '1px #cc4c4c solid',
   'color' : '#cc4c4c'})
        var scale = $(this).data('scale')
        $.each(data,function(i,data){
            if (scale == data.root) {
                $('#total-scale-men').append('<button class="scale-btn" data-css="'+ data.CSS +'">' + data.scale + '</button>');
            }
        });

        $('.scale-btn').on("click", function () {
    $('.nt').hide()
    $('.scales-men').hide()
    $('.gcMen ul li button').css({'border' : '1px #666 solid','color' : '#666'})

      var css = $(this).data('css')
      $.each(data,function(i,data){
        var notes = [data.noteClass_1,
                     data.noteClass_2,
                     data.noteClass_3,
                     data.noteClass_4,
                     data.noteClass_5,
                     data.noteClass_6,
                     data.noteClass_7,
                     data.noteClass_8]
        var ntClass = ['.A','.As','.B','.C','.Cs','.D','.Ds','.E','.F','.Fs','.G','.Gs']
      if (css == data.CSS) {
        $('#inf div:nth-child(1)').text(data.root)
        $('#inf div:nth-child(2)').text(data.scale)
        $('#inf div:nth-child(3)').text(data.structure)
        if (notes[0] == 'A') {
          $(ntClass[0]).fadeIn()
        }
      }
      });
  })

    })
}); 

You can observe that I stored the noteClass string values and div class values in arrays - now, how do I compare all values in the two arrays (notes & ntClass) and fade in the corresponding class when there's a match? That's the question at hand.

If my explanation wasn't clear enough, please check out the codepen for better clarity (http://codepen.io/JTBennett/pen/NdLWJy)

Any assistance provided would be greatly appreciated!

Answer â„–1

If I have interpreted your question correctly, it sounds like you are trying to achieve the following:

$(notes[0]).show();

As the only variance between your notes and classes is the presence of a dot before the class names, this code snippet should achieve what you're looking for.

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

Encountering a NoClassDefFoundError for JsonStructureBodyReader while using GlassFish 4.0 / Jersey 2.0

While working with JAX-RS on GlassFish 4.0 R89, I encountered a strange issue. Despite receiving XML responses from the server, I kept getting a NoClassDefFoundError for a JsonStructureBodyReader class (even though JSON was not being used). This problem pe ...

The Protractor test scripts are running with lightning speed, outpacing the webpage loading time

Seeking guidance on automating an angular js website with login functionality. Need to click on the sign-in link and enter username and password, but facing issues with script execution speed being faster than page load. Here is my approach for handling th ...

Creating a series of scalable svgs of uniform size in a row, enabling me to resize them based on width without disrupting the overall design

I need a row of equally sized SVGs with some text below them, like a navigation bar but larger. I almost have it working, but there are some issues with my current setup. To clarify, this is what I am aiming for: The SVGs should be responsive and scale a ...

Converting YAML to JSON using Groovy

I'm in need of assistance with converting a yaml file to JSON format within a Jenkins pipeline that is scripted in Groovy. The goal is to parse the YAML content in its JSON format. Within the Groovy script, I have defined a variable called 'data ...

Iterate over the keys within a JSON array that begin with a certain key name

Is there a more efficient way to dynamically loop through keys in a JSON array starting with 'option' and either loop through them all or set a maximum limit? Currently, I am using the following approach: $.each(shopifyProductJSON.variants, fun ...

Achieving Harmony: Integrating Spring RestTemplate, Scala, and Jackson

Is it possible to utilize Jackson in order to create a mapper that converts Json-String to scala.collection.Map? How can I integrate this same mapper with a RestTemplate? val restTemplate = new RestTemplate() val module = new OptionModule with MapMo ...

Checking the response from an AJAX call with an if/else statement

Is there a way to create a counter for unread messages using PHP and jQuery? Below is the PHP code in BubbleStat.php: $totalMsg = $mysql->totalRows("SELECT msg_id from messages WHERE msg_opened = 0 AND msg_receiver = '".$_SESSION["ActiveUserSessio ...

Incorporate a lightbox within a repeater field in advanced custom fields

I have developed a gallery with dynamic pills using advanced custom fields and I need to add a lightbox to it. I've tried several times to add the code for the lightbox but all my attempts have been unsuccessful. I have already added all the necessar ...

How to extract data from a JSON API using Flutter

I have a service that provides data through an API. My goal is to dynamically change the color of a box based on the presence of a specific string in the data received from the API. Currently, I am checking if the last value in the dataset matches a cert ...

Issues with alignment in Bootstrap Table columns

I'm facing an issue with formatting a table properly. Despite adjusting the colspans, I can't seem to get it right. There's an additional column header on the right labeled "Operations" that is extending outside the div, making it impossible ...

differences between using form's get method and sending an angular $http.get request

When trying to make a GET request to a specific URL from my Angular frontend to an ExpressJS backend, I encountered some interesting behavior. In the frontend code snippet below: <li> <a ng-click="givequiz()">GiveQuiz</a> </l ...

Attempting to relocate various containers

My task involves handling a group of randomly placed boxes on a webpage, each painted in random colors. I am currently attempting to enable their movement from one location to another. Despite being a seemingly simple task, my lack of familiarity with mous ...

What is the best way to test a route using nock and request-promise when the URL includes single quotes?

Trying to test an API call using nock + request-promise is resulting in an error due to mismatched routes caused by single quotes in the API's url. The problem arises from request-promise url encoding the quotes while Nock does not. You can view the ...

What is the best way to rotate a mesh by 90 degrees in ThreeJS?

Currently, I'm working on rotating a mesh by 90 degrees within Three JS. Below is an image illustrating the current position: My goal is to have the selected mesh rotated parallel to the larger mesh. I attempted to rotate the matrix using the follow ...

Transforming a single object into multiple arrays using AngularJS

Just starting out with AngularJS and I've got some data that looks like this { day1: 0, day2: 0, day3: 0, day4: 2 } Is there a way to convert this data into arrays structured like below? [     ["day1": 0],     ["day2": 0],   ...

What is the best way to manage the gradual disappearance of multiple divs belonging to the same class using jQuery?

I need help with a hover effect for 6 divs on a page. Each div has a layer on top that fades out on hover and fades back in on mouseleave. I currently have code for each individual div layer, but it's causing a stack overrun. How can I assign this fun ...

Vue.js navigation guards, restrict access to all unauthorized routes, grant entry to specific routes upon successful authentication

I'm currently working on implementing a navigation guard in Vue.js with a specific logic: I want to restrict access to all routes that require authentication and redirect users to the login page if they are not authenticated. The only exception is the ...

Is it possible to automatically refresh the Next Auth Session upon page reloads using API data updates?

There is a scenario I'm facing where I need to ensure the Next Auth Session is updated every time the page reloads in order to reflect the latest "Users" Details. My requirement involves making an API call upon each page reload to fetch the Users dat ...

Bizarre error in rendering angular template

Included in my index.html file is the following code: ...... <my-test-app></my-test-app> <script src="bower_components/angular/angular.js"></script> <script src="bower_components/angular-component-router/angular_1_router.js"> ...

Tips for accessing an array you've constructed in the $onInit() function within Angular-Fullstack

I am currently working on an Angular fullstack project that utilizes Babel and Angular 1.5.0. The problem I am encountering is that I am trying to initialize an array (this.events = []) but unable to access this array in $onInit() where I need to populate ...