0

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]
2
  • when using arrow function, and using expression inside you'll have to mention return explicity Commented Jul 19, 2018 at 7:29
  • BTW your question title is a bit off. Commented Jul 19, 2018 at 7:31

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.