jQuery匹配值 [英] Jquery matching values
问题描述
我正在尝试执行if语句,以查看值是否在数组中完全匹配.然后,我希望它的兄弟元素显示确切匹配的html.有人可以帮忙吗!我将TWIG用于高级自定义字段Wordpress插件的HTML. 我只想说一次的所有地点的图片及其被提及的次数.这是用于过滤器功能的.最终希望有一个像> https://codepen.io/luciopaiva/pen/YXYGYE的下拉列表?editors = 101 (带有位置"标签).
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>
推荐答案
因此,我将使用 .map
,然后 .reduce
将其转换为格式为{ city: count }
的对象.最后,您可以创建html元素并将其插入dom.
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>
这篇关于jQuery匹配值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!