在jQuery过滤器函数中使用动态创建的变量 [英] Use dynamically created variables in jQuery filter function
问题描述
Meanwhile know how to create a Count based list of Variables (see Create dynamic variable names based on count result).
但是现在我还需要知道如何在到目前为止看起来像这样的过滤器函数中使用此动态变量列表:
But now I also need to know how to use this dynamic list of variables inside a filter function that looks like this so far:
$("div.item")
.filter(function( index ) {
return $(this).data("sample1") == samplevariable1 &&
$(this).data("muster1") == mustervariable1;
}).css( "border", "3px double red" );
可以有1到x个动态创建的变量,而不是SampleVariable1,SampleVariable2.
There can be 1 to x dynamically created Variables instead of SampleVariable1, SampleVariable2.
如何重写现有的过滤器函数,以便可以根据需要将其用于尽可能多的变量?
How can I rewrite my existing filter function so that I can use it for as many variables as needed?
谢谢您的帮助!
JSFiddle: https://jsfiddle.net/SchweizerSchoggi/30od7vf8/58/
推荐答案
假设您要基于匹配某些生成的值来突出显示DIV列表
在基于您提供的小提琴的以下解决方案中,您将找到:
Supposed you want to highlight a DIV-list based on matching some generated values
In the following solution based on your provided fiddle you will find:
- 生成器功能,该函数用连续的数字填充两个数组(最多生成N个值).请参阅对您相应问题
数组方法 #54746262>根据计数结果创建动态变量名称 - 修改功能,用于模拟先前生成的值的某些更改(因为您所说的脚本中可能会发生这种情况)
- 一个过滤和突出显示功能,该功能使用 jQuery (用于具有"item"类的DIV元素)选择HTML元素,并根据其数据匹配来对其进行过滤-带有相应的数组元素.过滤后的HTML元素将使用指定的CSS边框突出显示.
- a generator-function which fills two arrays with consecutive numbers (generates up to N values). See answered array-approach to your corresponding question Create dynamic variable names based on count result
- a modification-function which simulates some changes of the previously generated values (as this could happen in your script as you said)
- a filter-and-highlight-function which selects HTML-elements using jQuery (for DIV elements with class 'item') and filter them based on matching their DATA-attributes with the corresponding array-elements. This filtered HTML-elements will be highlighted using specified CSS-border.
- JS中的数组通常以0(第一个元素)开始到N(其中数组有N + 1个元素)结束索引. HTML的DIV元素(即
- 您的数据属性从1开始被索引.因此,该解决方案定义了一个特殊的索引变量:
let divIndex = i+1;
data-sample1
或data-muster1
)中的- An array in JS usually is indexed starting with 0 (first element) and ending with N (where array has N+1 elements).
- Your data-attributes inside the HTML's DIV-elements (i.e.
data-sample1
ordata-muster1
) are indexed starting with 1. So the solution has defined a special index-variable:let divIndex = i+1;
/* N: used to generate N initial values as arrays */
const SEGMENT_COUNT = 2;
/* contains two dynamically generated arrays */
var generatedData = {elementsCount: 0, sample: [], muster: []};
/* Generate dynamic variables:
Creates two arrays named 'sample' and
muster' inside sampleMusterArrays. These arrays are each filled with elementsCount elements (so that elements can be index from 0 to elementsCount-1 ). */
function generateValues(sampleMusterArrays, elementsCount) {
for (let i = 0; i < elementsCount; i++) {
sampleMusterArrays.sample[i] = i+1;
sampleMusterArrays.muster[i] = i+1;
}
sampleMusterArrays.elementsCount = elementsCount;
}
function modifyGeneratedValues() {
// modify FIRST value in sample-array
generatedData.sample[0] = '2';
// modify SECOND value in sample-array
generatedData.sample[1] = 'x';
// modify FIRST value in muster-array
generatedData.muster[0] = '3';
// modify SECOND value in muster-array
generatedData.muster[1] = 'x';
}
/* filter DIVs with class "item" based on matching sampleMusterArrays to corresponding data-values */
function highlightFilteredDivItems(sampleMusterArrays) {
$( "div.item" )
.filter(function( index ) {
let foundMatch = false;
for (var i=0; i < sampleMusterArrays.elementsCount; i++) {
let divIndex = i+1;
foundMatch = foundMatch ||
$(this).data("sample"+divIndex) == sampleMusterArrays.sample[i] &&
$(this).data("muster"+divIndex) == sampleMusterArrays.muster[i];
} // end for-loop
return foundMatch;
}).css( "border", "3px double red" );
}
/* MAIN */
// generate data
generateValues(generatedData, SEGMENT_COUNT);
console.log("--- generated values: ", generatedData);
/* do other stuff in between */
/* e.g. arbitratily modify generated data */
modifyGeneratedValues();
console.log("--- modified generated values: ", generatedData);
// only the first Div should be marked
highlightFilteredDivItems(generatedData);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="item" data-sample1="2" data-muster1="3">Div 1</div>
<div class="item" data-sample1="3" data-muster1="3">Div 2</div>
<div class="item" data-sample1="4" data-muster1="3">Div 3</div>
这篇关于在jQuery过滤器函数中使用动态创建的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!