基于键如何在JavaScript中从数组中过滤?

时间:2018-07-19 07:21:32

标签: javascript arrays

我收到所有工作单的回复。

{
  "jobOrders": [{
      "id": "5b4f7ad860dfee3b009d7452",
      "haulier": {
        "companyName": "BigDataMatica",
        "email": "nirmalkumar.s@datinfi.com",
        "registrationNumber": "nirmal89HJ",
        "companyAddress": "RSPURAM",
        "companyPhone": "8687678",
        "yardAddress": "Pragatinagar",
        "yardPhone": "69876876",
        "haulierCode": "Haulier",
        "billingAddress": "Pragatinagar"
      }
    },
    {
      "id": "5b501f8f60dfee3b009d7454",
      "haulier": {
        "companyName": "BigDataMatica",
        "email": "nirmalkumar.s@datinfi.com",
        "registrationNumber": "nirmal89HJ",
        "companyAddress": "RS PURAM",
        "companyPhone": "8687678",
        "yardAddress": "Pragatinagar",
        "yardPhone": "69876876",
        "haulierCode": "Haulier",
        "billingAddress": "Pragatinagar"
      }
    },
    {
      "id": "5b5020f360dfee3b009d7455",
      "haulier": {
        "companyName": "BigDataMatica",
        "email": "sivasai.s@datinfi.com",
        "registrationNumber": "nirmal89HJ",
        "companyAddress": "RS PURAM",
        "companyPhone": "8687678",
        "yardAddress": "Pragatinagar",
        "yardPhone": "69876876",
        "haulierCode": "Haulier",
        "billingAddress": "Pragatinagar"
      }
    }
  ]
}

基于工作订单,我需要基于haulier对象email键来过滤工作订单。

let haulierjobordersnames = joborderlist && joborderlist.map && joborderlist.map(a => a.haulier.email);
console.log("haulierjobordersnames", haulierjobordersnames);

输出:

["nirmalkumar.s@datinfi.com", "nirmalkumar.s@datinfi.com", "sivasai.s@datinfi.com"]

let haulierjoborders = joborderlist && joborderlist.map && joborderlist.map((el)=>{el.haulier.email == haulierjobordersnames})
console.log("haulierjoborders", haulierjoborders);

输出:

[undefined, undefined, undefined]

2 个答案:

答案 0 :(得分:2)

两个问题:

  1. 如果需要过滤,则需要致电.filter,而不是(仅)致电.map
  2. 如果在回调函数中使用花括号,则将其视为代码块,因此您需要return。否则,请跳过花括号,以便使用表达式语法。

所以:

joborderlist.map((el)=>{el.haulier.email == haulierjobordersnames})

应成为:

joborderlist.filter((el)=>el.haulier.email == haulierjobordersnames)
            .map((el) => el.haulier.email)

很显然,通过这种方式,输出将具有相同的重复值,因此也许您希望提取除刚刚过滤的电子邮件之外的其他信息。

答案 1 :(得分:0)

您需要的是Call: mle(minuslogl = ll, start = list(b = 2, d = 1.5), method = "L-BFGS-B", nobs = NROW(rn), lower = 1, upper = 20) Coefficients: b d 1.447694 20.000000 ,而不是filter。另外,您还需要从过滤器函数中返回该值。

map