I have a table with products and it's variants.
我有一个产品表和它的变种。
Each one has an index on the table, for instance:
每个表都有一个索引,例如:
[0] Product 1
[1] Variant 1
[2] Variant 2
[3] Product 2
etc..
I've assigned to an associative array all the variant's indexes along with their value, for instance:
我已将所有变体索引及其值分配给关联数组,例如:
[1] Variant 1 : 13
[2] Variant 2 : 15
[18] Variant 3: 32
Now I want to loop through the table using only the indexes I got on my array.
现在我想只使用我在数组上得到的索引遍历表。
Is there a way to loop through an element using specific indexes? ( So I don't have to loop through the whole thing and execute what I want whenever it matches ). Something similar to this pseudocode:
有没有办法使用特定索引循环元素? (所以我不必遍历整个事情并在匹配时执行我想要的东西)。与此伪代码类似的东西:
loop through my_table on index = [1,2,18]
循环遍历my_table on index = [1,2,18]
Edit for Joseph Marikle:
编辑Joseph Marikle:
The code part for the array is this:
数组的代码部分是这样的:
var variants_index = [];
$.each($('.variant'),function(){
variants_index[$(this).index()] = $(this).find('td').eq(1).text();
});
typing typeof(variants_index)
we get: object
and typing the var in the console, we get Array
and, typing Array.isArray(variants_index)
we get true
输入typeof(variants_index)我们得到:对象并在控制台中键入var,我们得到数组,然后输入Array.isArray(variants_index)我们得到的
2 个解决方案
#1
1
You can loop through your array of indices that you are interested in and use their values as an index to your array containing product info.
您可以遍历您感兴趣的索引数组,并将它们的值用作包含产品信息的数组的索引。
var indexToCheck = [1,2,18];
for(var i = 0; i < indexToCheck.length; i++){
var productInfo = myProducts[indexToCheck[i]];
}
#2
1
loop through my_table on index = [1,2,8] (changed the 18 to 8 for simplicity)
在index = [1,2,8]上循环遍历my_table(为简单起见,将18更改为8)
Since that's the core of the problem, let's try to solve it.
由于这是问题的核心,让我们试着解决它。
index = [1,2,8]
my_table = [5,4,6,7,3,8,2,9,0,1]
for (i=0;i<index.length;i++){
console.log(my_table[index[i]]);
}
This would get the numbers from "index" as use them as indexes to access positions in my_table
这将使“index”中的数字用作索引来访问my_table中的位置
#1
1
You can loop through your array of indices that you are interested in and use their values as an index to your array containing product info.
您可以遍历您感兴趣的索引数组,并将它们的值用作包含产品信息的数组的索引。
var indexToCheck = [1,2,18];
for(var i = 0; i < indexToCheck.length; i++){
var productInfo = myProducts[indexToCheck[i]];
}
#2
1
loop through my_table on index = [1,2,8] (changed the 18 to 8 for simplicity)
在index = [1,2,8]上循环遍历my_table(为简单起见,将18更改为8)
Since that's the core of the problem, let's try to solve it.
由于这是问题的核心,让我们试着解决它。
index = [1,2,8]
my_table = [5,4,6,7,3,8,2,9,0,1]
for (i=0;i<index.length;i++){
console.log(my_table[index[i]]);
}
This would get the numbers from "index" as use them as indexes to access positions in my_table
这将使“index”中的数字用作索引来访问my_table中的位置