Jquery 个匹配值

Jquery matching values

您好,我正在尝试执行 if 语句来查看数组中的值是否完全匹配。然后我希望它的兄弟元素显示完全匹配的 html 。有人可以帮忙吗!我将 TWIG 用于 Advanced Custom Fields Wordpress 插件中的 HTML。 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 带有位置选项卡。

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>

所以我会使用 .map and then .reduce 将所有值收集到格式为 { city: count } 的对象中。最后,您可以创建 html 元素并将它们插入 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>