0

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?

3
  • to compare 2 arrays and create a 3rd array of all the common items take element 1 from A and check if it is in B. If yes, put that in C. Do the same with B to A. Commented Mar 5, 2019 at 21:54
  • Your example input/output shows that you want to merge arrays without duplicates, not gather common elements. Commented Mar 5, 2019 at 21:56
  • That is partially true, I want to combine the two arrays, but also leave out any thing that does not have a common element (in this case the state) that is not in the other array. (I will edit the OP to clarify) Commented Mar 5, 2019 at 21:58

1 Answer 1

1
            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.

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

4 Comments

I get an error with the following code: `let finalOffice = this.employees.filter((state: any) => !this.offices.include(state)); console.log(finalOffice);
@VanCowboy We can't add the remaining code in the comment section Van. You need to add the code in your question.
@VanCowboy Your JSON is not in the correct format you need to provide key and values in your array of objects.
@VanCowboy Hope this helps I tried to work out other ways but I believe this is the best way for this one. Let me know if it does not work. I shall revisit the code.

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.