jQuery:如何为数组中的每个项创建变量

时间:2021-11-05 14:20:40

I am creating an array with the below code.

我正在使用以下代码创建一个数组。

The single functions work fine so far, my only problem with the loop is how to create the variables (nameX and dataX) dynamically from each item in the array.

到目前为止,单个函数工作正常,我对循环的唯一问题是如何从数组中的每个项动态创建变量(nameX和dataX)。

Example: My array is [item1,item2,item3]. In this case the variables should be name1, data1, name2, data2, name3, data3 (nameX and dataX are just placeholders here for now).

示例:我的数组是[item1,item2,item3]。在这种情况下,变量应该是name1,data1,name2,data2,name3,data3(nameX和dataX现在只是占位符)。

My code:

我的代码:

//get unique items
var inputSearch = new Array();
$('td.searchTerm').each(function() {
    if( ($(this).text() != '') && ($(this).text() != ' ') ) {
        if(inputSearch.indexOf($(this).text()) == -1) {
            inputSearch.push( $(this).text() );
        }
    }
});

//loop through array to get volumes per item
var i;
for (i = 0; i < inputSearch.length; i++) {
    var nameX = inputSearch[i].replace(/\s+/g, '');
    var dataX = [];
    var xSum = 0;
    $('#myTable tbody tr').each(function() {
        if ($(this).find('td:contains(' + nameX + ')').length === 0) {
            dataX.push(0);
        } else {
            xSum = 0;
            $(this).find('td:contains(' + nameX + ')').each(function() {
                 xSum += parseInt($(this).next('td').text());
            });

            dataX.push(xSum);
        }
    });
}

Update: I found a way to create the name variable dynamically, so now only the data variable is missing:

更新:我找到了一种动态创建名称变量的方法,所以现在只缺少数据变量:

window['name' + i.toString()] = inputSearch[i].replace(/\s+/g, '');

Many thanks for any help with this, Tim.

蒂姆,非常感谢你提供的任何帮助。

1 个解决方案

#1


1  

As I said in the comments, don't try to create "variable variables". Only pain and suffering lies down that path.

正如我在评论中所说,不要试图创建“变量变量”。这条道路上只有痛苦和痛苦。

Here is a way to solve what you seem to be trying without resorting to ugly hacks.

这是一种解决你似乎尝试的方法,而不诉诸丑陋的黑客。

var searchTerm = {};

$('td.searchTerm').each(function () {
    var term = $.trim( $(this).text() ), 
        name = term.replace(/\s+/g, '_');

    // prevent empty or non-unique entries
    if ( !term || searchTerm.hasOwnProperty(term) ) return;

    // sum up table values for the current term
    searchTerm[term] = 0;
    $('#myTable tbody td:contains(' + name + ')').each(function () {
        searchTerm[term] += parseInt( $(this).next('td').text(), 10 );
    });
});

Would give you something like this:

会给你这样的东西:

searchTerm = {
    "foo": 10,
    "bar": 20,
    "baz": 0
};

Think about handling mixed case appropriately. Currently foo and Foo are different terms, which might or might not be what you need.

考虑适当处理混合案例。目前foo和Foo是不同的术语,可能是也可能不是你需要的术语。

There is an easy way to extend jQuery with a case-insensitive :contains selector.

有一种简单的方法可以使用不区分大小写的方式扩展jQuery:contains selector。

#1


1  

As I said in the comments, don't try to create "variable variables". Only pain and suffering lies down that path.

正如我在评论中所说,不要试图创建“变量变量”。这条道路上只有痛苦和痛苦。

Here is a way to solve what you seem to be trying without resorting to ugly hacks.

这是一种解决你似乎尝试的方法,而不诉诸丑陋的黑客。

var searchTerm = {};

$('td.searchTerm').each(function () {
    var term = $.trim( $(this).text() ), 
        name = term.replace(/\s+/g, '_');

    // prevent empty or non-unique entries
    if ( !term || searchTerm.hasOwnProperty(term) ) return;

    // sum up table values for the current term
    searchTerm[term] = 0;
    $('#myTable tbody td:contains(' + name + ')').each(function () {
        searchTerm[term] += parseInt( $(this).next('td').text(), 10 );
    });
});

Would give you something like this:

会给你这样的东西:

searchTerm = {
    "foo": 10,
    "bar": 20,
    "baz": 0
};

Think about handling mixed case appropriately. Currently foo and Foo are different terms, which might or might not be what you need.

考虑适当处理混合案例。目前foo和Foo是不同的术语,可能是也可能不是你需要的术语。

There is an easy way to extend jQuery with a case-insensitive :contains selector.

有一种简单的方法可以使用不区分大小写的方式扩展jQuery:contains selector。