VanCowboy
VanCowboy

Reputation: 220

Comparing 2 arrays to create a 3rd array of common items in Angular

I am trying to figure out the TypeScript logic to compare 2 arrays and create a 3rd array of all the common items.

i.e.

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

}

Ideally the third array would be something like:

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

Note that there is a duplicate of Nebraska, one for each person, and there is no listing for California as there no employee there and not listing for Bill Cody as there is no office in Wyoming.

Any suggestions on where I can find information on this?

Upvotes: 0

Views: 237

Answers (1)

Abhijeet Chakravorty
Abhijeet Chakravorty

Reputation: 227

            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 Hampshire", city: "Manchester" },
                  { state: "California", city: "Redding" }
            ]

            let finalArr = [];
            let self = this;
            for (let g=0;g<self.employees.length;g++) {
                    for (let h=0;h<self.offices.length;h++) {
                            if (self.employees[g]['state'] === self.offices[h]['state']) {
                                    finalArr.push(self.employees[g]);
                                    finalArr[finalArr.length - 1]['city'] = self.offices[h]['city'];
                                    break;
                            }
                    }
            }

console.log(finalArr);

You can try something like this.

Upvotes: 1

Related Questions