Jquery matching values

[亡魂溺海] 提交于 2021-02-05 10:59:06

问题


Hi I am trying to do an if statement to see if values have an exact match in an array. Then I want it the sibling element to show the html of what the exact match is. Can someone help please! I use TWIG for the HTML from Advanced Custom Fields Wordpress plugin. Image of all the locations that I want to say only once with the number of times they are mentioned. This is for a filter functionality. Eventually want to have a dropdown like https://codepen.io/luciopaiva/pen/YXYGYE?editors=101 with a locations tab.

Jquery

 //exact match
 $(".filter-dropdown .course-location").each(function(i,e) {
  myDivObj = $(this).val() == $(this).val();
    if ($(this).val() == $(this).val()) {
      console.log()
      $(this).parent('.filter-dropdown').siblings('.class-location').children('h4').html(myDivObj);
      $(this).parent('.filter-dropdown').siblings('.class-location').children('span').html($(".course-location").length);
    }
    else {

      console.log('unknown');
    }
  });

HTML

             <div class="filter" id="course-location">
              <div class="upper-filter"><h3>Locations</h3><i class="fas fa-angle-down"></i></div>
              {% for category in bloc.training_course_list_compact.course_categories %}
              {% for locationcourse in category.get_field('courses') %}
              {% for location in locationcourse.get_field('dates') %}
              <div class="filter-dropdown">
                <div class="course-location" value="{{ location.location }}"></div>
              </div>
              {% endfor %}
              {% endfor %}
              {% endfor %}
                <div class="class-location"><h4></h4><p></p><span></span></div>
            </div>

回答1:


So I'd collect all values using .map and then .reduce it into an object with the format { city: count }. Lastly, you can create html elements and insert them in the dom.

// Extract the values to an array
let locations = $('.course-location').map((i, e) => $(e).data('value')).toArray();

let reducer = (a, c) => {
  // If the city doesn't exist 
  a[c] === undefined ?
    a[c] = 1 : // One occurrence, else
    a[c] = a[c] + 1;  // Increment count
  return a;
}

let location_count = locations.reduce(reducer, {});

// Create HTML Elements
for (var place in location_count) {
  $('.class-location').append(`<h4>${place}</h4><span>${location_count[place]}</span>`)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="course-location" data-value="Crowley, Texas"></div>
<div class="course-location" data-value="Denver, Colorado"></div>
<div class="course-location" data-value="Houston, TX"></div>
<div class="course-location" data-value="Denver, Colorado"></div>
<div class="course-location" data-value="Dallas, Texas"></div>
<div class="course-location" data-value="Crowley, Texas"></div>

 <div class="class-location"></div>


来源:https://stackoverflow.com/questions/61467823/jquery-matching-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!