Home > Software engineering >  How to work with 2 arrays having field from one array dependent on field from another array?
How to work with 2 arrays having field from one array dependent on field from another array?

Time:04-05

I have two arrays namely arrayGuest and arrayRent. These are arrays of objects. Both objects and consequently, arrays have one field common that is GuestID. I want to create a list that when both guestID's match, displays fields from both arrays together.

These are the two objects:

class Guest
{
    guestID: number;
    firstName: string;
    lastName: string;
    panCardNumber: string;
    address: string;
    city: string;
    state: string;
    typeOfRoom: string;
}


class Rent
{
    guestID:number;
    amount:number;
    dateOfPayment: Date;
}

and here are the two arrays:

let arrayGuests:  Array<Guest> = [
    {guestID:1,firstName:"Jay",lastName:"Shetty",panCardNumber:"FSDDE2235A",address:"150ft Ring Road",city:"Rajkot",state:"Gujarat", typeOfRoom: roomType.AC},
    {guestID:2,firstName:"Pooja",lastName:"Mehta", panCardNumber:"FDSSQ5464J", address:"Bopal",city:"Ahmedabad",state:"Gujaray",typeOfRoom:roomType.NonAC},
    {guestID:3,firstName:"Rahul",lastName:"Joshi", panCardNumber:"ERTEQ2316H", address:"Thaltej",city:"Ahmedabad",state:"Gujarat",typeOfRoom:roomType.AC},
    {guestID:4,firstName:"Komal",lastName:"Kaushik", panCardNumber:"VBCCK7418Q", address:"Vasai",city:"Mumbai",state:"Gujarat",typeOfRoom:roomType.AC},
    {guestID:5,firstName:"Raj",lastName:"Khatri", panCardNumber:"KOILE5951B", address:"Connaught Place",city:"Delhi",state:"Delhi",typeOfRoom:roomType.NonAC}
]


let rentArray : Array<Rent>= [
    {guestID:1,amount:5000,dateOfPayment:new Date("2022-03-01")},
    {guestID:2,amount:5000,dateOfPayment:new Date("2022-03-01")},
    {guestID:3,amount:5000,dateOfPayment:new Date("2022-03-01")},
    {guestID:4,amount:5000,dateOfPayment:new Date("2022-03-01")},
    {guestID:5,amount:5000,dateOfPayment:new Date("2022-03-01")},
]

I want a result somewhat like, GuestID:1, Name : xyz, amount = 5000

CodePudding user response:

Since you said both the arrays are sorted, that means that we can go by the index value and simply merge them.

interface Guest {
    guestID: number;
    firstName: string;
    lastName: string;
    panCardNumber: string;
    address: string;
    city: string;
    state: string;
    typeOfRoom: string;
}

interface Rent {
    guestID: number;
    amount: number;
    dateOfPayment: Date;
}

type Merged = Guest & Rent;

const arrayGuests: Array<Guest> = [
    {
        guestID: 1,
        firstName: 'Jay',
        lastName: 'Shetty',
        panCardNumber: 'FSDDE2235A',
        address: '150ft Ring Road',
        city: 'Rajkot',
        state: 'Gujarat',
        typeOfRoom: 'AC',
    },
    {
        guestID: 2,
        firstName: 'Pooja',
        lastName: 'Mehta',
        panCardNumber: 'FDSSQ5464J',
        address: 'Bopal',
        city: 'Ahmedabad',
        state: 'Gujaray',
        typeOfRoom: 'NoneAC',
    },
    {
        guestID: 3,
        firstName: 'Rahul',
        lastName: 'Joshi',
        panCardNumber: 'ERTEQ2316H',
        address: 'Thaltej',
        city: 'Ahmedabad',
        state: 'Gujarat',
        typeOfRoom: 'AC',
    },
    {
        guestID: 4,
        firstName: 'Komal',
        lastName: 'Kaushik',
        panCardNumber: 'VBCCK7418Q',
        address: 'Vasai',
        city: 'Mumbai',
        state: 'Gujarat',
        typeOfRoom: 'AC',
    },
    {
        guestID: 5,
        firstName: 'Raj',
        lastName: 'Khatri',
        panCardNumber: 'KOILE5951B',
        address: 'Connaught Place',
        city: 'Delhi',
        state: 'Delhi',
        typeOfRoom: 'NonAC',
    },
];

const rentArray: Array<Rent> = [
    { guestID: 1, amount: 5000, dateOfPayment: new Date('2022-03-01') },
    { guestID: 2, amount: 5000, dateOfPayment: new Date('2022-03-01') },
    { guestID: 3, amount: 5000, dateOfPayment: new Date('2022-03-01') },
    { guestID: 4, amount: 5000, dateOfPayment: new Date('2022-03-01') },
    { guestID: 5, amount: 5000, dateOfPayment: new Date('2022-03-01') },
];

// Create a function which takes our guest array
// and rent array as arguments
const mergeArrays = (guests: Guest[], rent: Rent[]): Merged[] => {
    // Immediately return a new array, which will be
    // returned by the .map method
    return guests.map((guest, i) => {
        // Whatever is returned from each iteration becomes the
        // new value at that position in the array.
        // Return new object with all of our guest item properties,
        // Spread out all rent properties for the matching index.
        return {
            ...guest,
            ...rent[i],
        };
    });
};

console.log(mergeArrays(arrayGuests, rentArray));

Compiled:

"use strict";
const arrayGuests = [
    {
        guestID: 1,
        firstName: 'Jay',
        lastName: 'Shetty',
        panCardNumber: 'FSDDE2235A',
        address: '150ft Ring Road',
        city: 'Rajkot',
        state: 'Gujarat',
        typeOfRoom: 'AC',
    },
    {
        guestID: 2,
        firstName: 'Pooja',
        lastName: 'Mehta',
        panCardNumber: 'FDSSQ5464J',
        address: 'Bopal',
        city: 'Ahmedabad',
        state: 'Gujaray',
        typeOfRoom: 'NoneAC',
    },
    {
        guestID: 3,
        firstName: 'Rahul',
        lastName: 'Joshi',
        panCardNumber: 'ERTEQ2316H',
        address: 'Thaltej',
        city: 'Ahmedabad',
        state: 'Gujarat',
        typeOfRoom: 'AC',
    },
    {
        guestID: 4,
        firstName: 'Komal',
        lastName: 'Kaushik',
        panCardNumber: 'VBCCK7418Q',
        address: 'Vasai',
        city: 'Mumbai',
        state: 'Gujarat',
        typeOfRoom: 'AC',
    },
    {
        guestID: 5,
        firstName: 'Raj',
        lastName: 'Khatri',
        panCardNumber: 'KOILE5951B',
        address: 'Connaught Place',
        city: 'Delhi',
        state: 'Delhi',
        typeOfRoom: 'NonAC',
    },
];
const rentArray = [
    { guestID: 1, amount: 5000, dateOfPayment: new Date('2022-03-01') },
    { guestID: 2, amount: 5000, dateOfPayment: new Date('2022-03-01') },
    { guestID: 3, amount: 5000, dateOfPayment: new Date('2022-03-01') },
    { guestID: 4, amount: 5000, dateOfPayment: new Date('2022-03-01') },
    { guestID: 5, amount: 5000, dateOfPayment: new Date('2022-03-01') },
];
const mergeArrays = (guests, rent) => {
    return guests.map((guest, i) => {
        return Object.assign(Object.assign({}, guest), rent[i]);
    });
};
console.log(mergeArrays(arrayGuests, rentArray));

Read about the Array.prototype.map() method

Learn about the ...Spread Operator

  • Related