开发者

jQuery 2 dimensional array - How to loop through it and make a list based on a condition to the array value

So here is my 2D array in Jquery.

var Co开发者_如何学运维des = [
    $.map($('*[id^="action"]:checked ~ *[id^="product"]'), function (item, idx){
        return $(item).val();
    }),
    $.map($('*[id^="action"]:checked'), function (item, idx) {
        return $(item).val();
    })
];

The array is something like this Codes["Apple"][101], ["Pear"][30] etc.,

I need to make a list of all the codes (eg., 101,1,3) for the product "Apple".

I am pretty new to jquery. Would appreciate any pointers.

Thanks


Since you include no HTML or actual data, we have to guess a little bit from your question, but it looks to me like your declaration of the Codes array creates an array of two arrays where the first array has products in it and the the second array has numbers in it like this:

var Codes = [["Apples", "Bananas", "Apples", "Artichokes"], [101, 30, 200, 29]];

And you want an array of numbers that correspond to only the Apples entries. You could do that like this:

var results = [];
for (var i = 0; i < Codes[0].length; i++) {
    if (Codes[0][i] == "Apples") {
        results.push(Codes[1][i]);
    }
}

Would give this result:

results == [101, 200]

This assumes that there is exactly one entry in the second array for every element in the first array and that like index items in the first array correspond to that element in the second array.


I might be misunderstanding your array, but try:

Codes["Apple"].join()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜