user9883888
user9883888

Reputation: 419

Based on key how to filter from array in JavaScript?

I am having a response of all the job orders.

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

Based on the job orders, I need to filter the job orders based on haulier object email key.

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

output:

["[email protected]", "[email protected]", "[email protected]"]

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

output:

[undefined, undefined, undefined]

Upvotes: 0

Views: 65

Answers (2)

Shubham Khatri
Shubham Khatri

Reputation: 282000

What you need is a filter and not map. Also you need to return the value from the filter function.

const joborderlist =  [
        {
            "id": "5b4f7ad860dfee3b009d7452",
            "haulier": {
                "companyName": "BigDataMatica",
                "email": "[email protected]",
                "registrationNumber": "nirmal89HJ",
                "companyAddress": "RSPURAM",
                "companyPhone": "8687678",
                "yardAddress": "Pragatinagar",
                "yardPhone": "69876876",
                "haulierCode": "Haulier",
                "billingAddress": "Pragatinagar"
            }
        },
        {
            "id": "5b501f8f60dfee3b009d7454",
            "haulier": {
                "companyName": "BigDataMatica",
                "email": "[email protected]",
                "registrationNumber": "nirmal89HJ",
                "companyAddress": "RS PURAM",
                "companyPhone": "8687678",
                "yardAddress": "Pragatinagar",
                "yardPhone": "69876876",
                "haulierCode": "Haulier",
                "billingAddress": "Pragatinagar"
            }
        },
        {
            "id": "5b5020f360dfee3b009d7455",
            "haulier": {
                "companyName": "BigDataMatica",
                "email": "[email protected]",
                "registrationNumber": "nirmal89HJ",
                "companyAddress": "RS PURAM",
                "companyPhone": "8687678",
                "yardAddress": "Pragatinagar",
                "yardPhone": "69876876",
                "haulierCode": "Haulier",
                "billingAddress": "Pragatinagar"
            }
        }
    ]

const haulierjobordersnames = '[email protected]';
let haulierjoborders = joborderlist && joborderlist.filter && joborderlist.filter((el)=>{ return el.haulier.email == haulierjobordersnames})
console.log("haulierjoborders", haulierjoborders);

Upvotes: 0

trincot
trincot

Reputation: 351228

Two issues:

  1. If you need to filter, you need to call .filter, not (only) .map
  2. If you use braces in the callback function, it is treated as a code block, so you need to return something. Otherwise skip the braces so you use the expression syntax.

So:

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

Should become:

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

Obviously, in this way the output will have a repetition of the same values, so maybe you want to extract some other information than the email you just filtered on.

Upvotes: 2

Related Questions