根据另一个值数组的存储项目 [英] Storing items of an array depending on another value
问题描述
我有一个数组称为目录持有4项:
I have an array called catalogue which holds 4 items:
包ID,数据ID,类别ID和datapackage。
packageid, dataid , categoryid and datapackage.
我想要做的是推到数组的数据ID 所有项目的包标识
的数据ID等于 packageBtnFilter
,其类别id
等于 categoryBtnFilter
。如果值 packageBtnFilter
和 categoryBtnFilter
都是0,那么就应该目录的所有数据ID推入数据ID
阵列。目前,它只是推动物品,其包ID等于3的数据ID
What I want to do is push into the array dataids all the dataids of items whose packageid
is equal to packageBtnFilter
and whose categoryid
is equal to categoryBtnFilter
. If the value of packageBtnFilter
and categoryBtnFilter
are both 0 then it should push all the dataids of catalogue into the dataids
array. At the moment it just pushes the dataids of items whose packageid is equal to 3.
var packageBtnFilter = $('#packages').val();
var categoryBtnFilter = $('#categories').val();
var dataids = new Array();
$(catalogue).each(function () {
if (this.packageid == 3) dataids.push(this.dataid);
});
谢谢!
全code是这里
http://jsfiddle.net/hunter/Tcwgf/1/
推荐答案
Use the filter function to filter the result and map to get the ids:
var dataids = (
categoryBtnFilter==0 && packageBtnFilter==0
? catalogue
: catalogue.filter(function(i) {
return i.categoryid=categoryBtnFilter && i.packageid==packageBtnFilter
})
).map(function(i){return i.dataid});
这篇关于根据另一个值数组的存储项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!