Jay Sardar
Jay Sardar

Reputation: 49

how to filter an element of array inside array of objects in java-script without using for loop?

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]"] };

Upvotes: 0

Views: 91

Answers (1)

DecPK
DecPK

Reputation: 25408

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

Upvotes: 1

Related Questions