I am currently working on a TypeScript function to compare two arrays and generate a third array containing the common items.
For example:
employees: any;
offices: any;
constructor() {
this.employees = [
{ fname: "John", lname: "James", state: "New York" },
{ fname: "John", lname: "Booth", state: "Nebraska" },
{ fname: "Steve", lname: "Smith", state: "Nebraska" },
{ fname: "Stephanie", lname: "Smith", state: "New Hampshire" },
{ fname: "Bill", lname: "Kydd", state: "New Mexico" },
{ fname: "Bill", lname: "Cody", state: "Wyoming" }
]
this.offices = [
{ state: "New York", city: "Albany" },
{ state: "Nebraska", city: "Omaha" },
{ state: "New Mexico", city: "Albuquerque" },
{ state: "New Hamshire", city: "Manchester" },
{ state: "California", city: "Redding" }
]
let finalOffice = this.employees.filter((state: any) => !this.offices.include(state));
console.log(finalOffice);
}
The desired result for the third array would be something similar to:
empofclist = [
{state: "New York", city: "Albany", fname: "John",lname: "James"},
{state: "Nebraska", city: "Omaha",fname: "John",lname: "Booth"},
{state: "Nebraska", city: "Omaha",fname: "Steve",lname: "Smith"},
{state: "New Mexico", city: "Albuquerque",fname: "Bill",lname: "Kydd"},
{state: "New Hamshire",city: "Manchester",fname: "Stephanie",lname: "Smith"}
]
It's worth noting that there are multiple entries for Nebraska, one for each person, and no entry for California as there are no employees there, and no listing for Bill Cody because there is no office in Wyoming.
If you have any recommendations on where I can gather more information on this topic, please let me know!