Looking for assistance with the syntax issue I'm encountering on this line:
arr2.some((arr2Obj) => arr2Obj[identifier] === arr1Obj[identifier]) // Type 'U' cannot be used to index type 'T'
I'm attempting to create a function that compares two arrays of objects to determine if a specific value (in this case "id") is present in both arrays.
type Member = {
id: string;
firstName: string;
lastName: string;
};
const signedIn = [
{
id: uniqid(),
firstName: "Joe",
lastName: "Bloggs"
}
];
const notSignedIn = [
{
id: uniqid(),
firstName: "Mary",
lastName: "Smith"
},
{
id: uniqid(),
firstName: "Trevor",
lastName: "Small"
}
];
function duplicateObjectInArrays<T, U>(
arr1: T[],
arr2: T[],
identifier: U
): boolean {
// return true if there is a dupe
return arr1.some((arr1Obj) =>
arr2.some((arr2Obj) => arr2Obj[identifier] === arr1Obj[identifier]) // Error's here
);
}
console.log(duplicateObjectInArrays<Member, string>(signedIn, notSignedIn, "id"));