如何过滤对象数组中的数组属性

时间:2016-05-03 18:25:41

标签: javascript arrays object filter

我有一个像这样初始化的集合:

var invoices = new Array();
var invoice = new Invoice();
invoice.number = "123";
invoice.date = "2016-05-03";
invoice.amount = "100";
var products = new Products();

var product = new Product();
product.code = "A";
product.name = "bar";
products.push(product);   

var product2 = new Product();
product2.code = "B";
product2.name = "foo";
products.push(product2);

invoice.products = products;

现在我要按照这样的发票属性进行过滤。

var filtered = invoices.filter(function(invoice){
   return invoice.number == "123";
});

但现在我想获得与编号和产品名称匹配的发票

我该怎么做

var filtered = invoices.filter(function(invoice){
   return invoice.number == "123" 
   // && invoice.products "name" == "foo";  //<-- At this level how can I filter?
});

3 个答案:

答案 0 :(得分:6)

使用Array.some检查是否存在具有所需名称的产品:

var filtered = invoices.filter(function(invoice){
   return invoice.number == "123" && invoice.products.some(function(prod) {
       return prod.name === 'foo';
   });
});

与使用Array.filter并检查生成的数组的length不同,这可以避免创建临时数组和短路;一旦找到命中,它立即返回true。

答案 1 :(得分:1)

var filtered = invoices.filter(function(invoice){
   return invoice.number == "123" && invoice.products.some(function (p) {
      return p.name == "foo";
   });
});

答案 2 :(得分:0)

如果您想获取包含已过滤产品的新发票清单,则需要克隆发票而不是将其与过滤器相关联:

    var filtered = []
    invoices.forEach(function (invoice) {
      if (invoice.number != "123") {
        return;
      }
      var newInvoice = new Invoice();
      newInvoice.number = invoice.number;
      newInvoice.date = invoice.date;
      newInvoice.amount = invoice.amount;
      var newProducts = invoice.products.filter(function (product) {
        return product.name == "foo"
      });
      newInvoice.products = newProducts
      filtered.push(newInvoice)
    });

UPD:正如约翰所说,如果您想通过以下规则过滤发票:&#34;获取编号等于123且至少有一个名为&#34; foo&#34;的产品的所有发票。这是解决方案:

    var filtered = invoices.filter(function(invoice){
       return invoice.number == "123" && invoice.products.some(function (p) {
          return p.name == "foo";
       });
    });