0

my data. data also has empty entries

const aadharData = [
  {
    firstName: "Ritam",
    lastName: "Singhal",
    address: {
      location: "rampur",
      state: "up",
    },
    hasDrivingLicense: false,
    emails: ["[email protected]", "[email protected]", "[email protected]"],
  },
  {
    firstName: "Siddarth",
    lastName: "Singhal",
    address: {
      location: "chandigarh",
      state: "haryana",
    },
    hasDrivingLicense: false,
    emails: ["[email protected]"],
  },
];

1. what I managed to filter out was a list of emails and fullname. with following code

let gmailList = [];
aadharData.forEach((info) => {
  if (info.emails) {
    let fullname = info.firstName + " " + info.lastName;
    let email = info.emails;
    gmailList.push({ fullname, email });
  }
});

2. I want to filter only those emailID that are gmails. however, I don't know how to iterate inside the property. I tried using forEach, some, and filter inside forEach but that doesn't seem to work or at least I don't know how to use them effectively. my assignment states I can't use for loop. finally, i want results in the format as

 { fullname, gmail } 

example

{ fullname: "Mayank Attri", email: ["[email protected]"] };

1 Answer 1

1

You can easily achieve the result using map and filter. You can filter the email as

o.emails && o.emails.filter((e) => e.match(/gmail.com$/))

const aadharData = [{
    firstName: "Ritam",
    lastName: "Singhal",
    address: {
      location: "rampur",
      state: "up",
    },
    hasDrivingLicense: false,
    emails: ["[email protected]", "[email protected]", "[email protected]"],
  },
  {
    firstName: "Siddarth",
    lastName: "Singhal",
    address: {
      location: "chandigarh",
      state: "haryana",
    },
    hasDrivingLicense: false,
    emails: ["[email protected]"],
  },
  {
    firstName: "Mukul",
    lastName: "Gaddiyan",
    address: {
      location: "lucknow",
      state: "up",
    },
    hasDrivingLicense: false,
  },
  {
    firstName: "Mayank",
    lastName: "Attri",
    address: {
      location: "alwar",
      state: "rajasthan",
    },
    hasDrivingLicense: true,
    emails: ["[email protected]"],
  },
  {
    firstName: "Akash",
    lastName: "Agarwal",
    address: {
      location: "gurgaon",
      state: "haryana",
    },
    hasDrivingLicense: true,
    emails: ["[email protected]", "[email protected]", "[email protected]"],
  },
];

const result = aadharData.map((o) => ({
  fullname: `${o.firstName} ${o.lastName}`,
  emails: o.emails && o.emails.filter((e) => e.match(/gmail.com$/)),
}));

console.log(result);
/* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */

.as-console-wrapper {
  max-height: 100% !important;
  top: 0;
}

Updated: If you want result your resultant array contains only emails if there are more than one emails

const aadharData = [{
    firstName: "Ritam",
    lastName: "Singhal",
    address: {
      location: "rampur",
      state: "up",
    },
    hasDrivingLicense: false,
    emails: ["[email protected]", "[email protected]", "[email protected]"],
  },
  {
    firstName: "Siddarth",
    lastName: "Singhal",
    address: {
      location: "chandigarh",
      state: "haryana",
    },
    hasDrivingLicense: false,
    emails: ["[email protected]"],
  },
  {
    firstName: "Mukul",
    lastName: "Gaddiyan",
    address: {
      location: "lucknow",
      state: "up",
    },
    hasDrivingLicense: false,
  },
  {
    firstName: "Mayank",
    lastName: "Attri",
    address: {
      location: "alwar",
      state: "rajasthan",
    },
    hasDrivingLicense: true,
    emails: ["[email protected]"],
  },
  {
    firstName: "Akash",
    lastName: "Agarwal",
    address: {
      location: "gurgaon",
      state: "haryana",
    },
    hasDrivingLicense: true,
    emails: ["[email protected]", "[email protected]", "[email protected]"],
  },
];

const result = aadharData.reduce((acc, curr) => {
  if (curr.emails) {
    const emails = curr.emails.filter((e) => e.match(/gmail.com$/));
    emails.length &&
      acc.push({
        fullname: `${curr.firstName} ${curr.lastName}`,
        emails,
      });
  }
  return acc;
}, []);

console.log(result);
/* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */

.as-console-wrapper {
  max-height: 100% !important;
  top: 0;
}

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

9 Comments

thank you for the answer. can you please tell me what you used ? the $ symbol i dont know what it is. if you can refer me some link it would be great help. also code runs here but on vs code it shows error TypeError: Cannot read property 'filter' of undefined at c:\Users\JAY SARDAR\project\es6\functionUp\firstproject\src\Routes\index.js:82:22
i got the similar error when i tried to use info.emails.some(condition) also i tried info.emails.filter, but i got same error TypeError: Cannot read property 'filter' of undefined
also its not specific to vs code. i got same error on codepen codepen.io/blue-berd/pen/PojqzdL
@JaySardar I've updated the answer, $ is used as Template string
Error was because some objects in array doesn't have the emails property.
|

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.