Open In App

How to Check if an Array Includes an Object in TypeScript ?

Last Updated : 04 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In TypeScript, checking if an array includes an object consists of comparing the object’s properties within the array elements. We can compare and check this using three different approaches some method, find method, and includes method.

There are several ways to check if an array includes an object in TypeScript which are as follows:

Using some method

In this approach, we are using some method with a callback function to iterate through the array and check if at least one element satisfies the condition where both the name and topic properties match those of the given object obj. The result is a boolean which represents whether the array includes the given object.

Syntax:

array.some(callback(element[, index[, array]])[, thisArg]); 

Example: The below example uses some method to check if an array includes an object in TypeScript.

Javascript




interface inter {
    name: string;
    topic: string;
}
const arr: inter[] = [
    { name: 'GFG1', topic: 'TypeScript' },
    { name: 'GFG2', topic: 'JavaScript' },
    { name: 'GFG3', topic: 'React' },
];
const obj: inter = { name: 'GFG2', topic: 'JavaScript' };
const res: boolean = arr
    .some(obj => obj
        .name === obj
            .name && obj
                .topic === obj
            .topic);
console.log(res);


Output:

true 

Using find method

In this approach, we are using the find method with a callback function to search for an element in the array that satisfies the condition where both the name and topic properties match those of the specified object obj. The result, stored in temp, will either be the matching object or undefined. The boolean variable res is then set to true if a matching object is found and false if not found.

Syntax:

array.find(callback(element[, index[, array]])[, thisArg]); 

Example: The below example usthe es find method to check if an array includes an object in TypeScript.

Javascript




interface inter {
    name: string;
    topic: string;
}
const arr: inter[] = [
    {
        name: 'GFG1',
        topic: 'TypeScript'
    },
    {
        name: 'GFG2',
        topic: 'JavaScript'
    },
    {
        name: 'GFG3',
        topic: 'React'
    },
];
const obj: inter =
{
    name: 'GFG2',
    topic: 'JavaScript'
};
const temp: inter | undefined = arr
    .find(
        obj => obj
            .name === obj
                .name && obj
                    .topic === obj
                .topic
    );
const res: boolean = !!temp;
console.log(res);


Output:

true 

Using includes method

In this approach, we are using the includes method with the map function and JSON.stringify to compare objects in the array. The map function is used to create an array of string representations of each object, and then the includes method checks if the string representation of the given object obj is present in the array.

Syntax:

array.includes(searchElement[, fromIndex]); 

Example: The below example uses includes method to check if an array includes an object in TypeScript.

Javascript




interface inter {
    name: string;
    topic: string;
}
const arr: inter[] = [
    {
        name: 'GFG1',
        topic: 'TypeScript'
    },
    {
        name: 'GFG2',
        topic: 'JavaScript'
    },
    {
        name: 'GFG3',
        topic: 'React'
    },
];
const obj: inter = { name: 'GFG', topic: 'JavaScript' };
const res: boolean = arr
    .map(obj => JSON
        .stringify(obj))
    .includes(JSON.stringify(obj));
console.log(res);


Output:

true 


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads