繁体   English   中英

通过jQuery中不同对象数组中的值过滤对象数组

[英]Filter an array of objects by a value from an array of different objects in jQuery

我是jQuery的新手,但很难找到答案。 我有两个阵列。

var arrayCountries = [{ Code:'NL', Name:'Netherlands'}, { Code:'BE', Name:'Belgium'}, ...];
var arrayCustomers = [{ Code:'C001', Country: 'NL', Group: 'A'}, { Code:'C002', Country: 'BE', Group: 'B'}, ...];

和一些代码来过滤数据。 当用户在“组”选择框中选择一个值时,我需要刷新两个选择框。 按预期填充第一个数组(customerData)。 但是我不知道如何填充第二个数组。 我尝试了很多方法,但没有任何喜悦。 countryData数组保持为空。

function filterFromGroupChanged(value){
    var customerData = $.grep(arrayCustomers, function (customer, i) { return arrayCustomers.Group === value; });
    refreshCustomers(customerData); // function to refresh a customer selectbox

    var countryData = $.grep(arrayCountries , function (country, i) {
            return $.grep(customerData, function (customer, i) { customer.Country=== country.Code; }).length > 0;
        });
        refreshCountries(countryData); // function to refresh a country select box
}

jQuery grep返回一个数组,因此,如果我测试它的长度,我应该能够返回true或false来满足第一个grep。 但它仍然是空的。

有人能指出我正确的方向吗?

看起来我很智障。 发布后,我再次查看了我的代码,看到了缺失的收益。 (位于CAPS下方,以便更好地在此处进行共享检测)

function filterFromGroupChanged(value){
    var customerData = $.grep(arrayCustomers, function (customer, i) { return arrayCustomers.Group === value; });
    refreshCustomers(customerData); // function to refresh a customer selectbox

    var countryData = $.grep(arrayCountries , function (country, i) {
            return $.grep(customerData, function (customer, i) { RETURN customer.Country=== country.Code; }).length > 0;
        });
        refreshCountries(countryData); // function to refresh a country select box
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM