How to Check if an Array Includes an Object in TypeScript ?
Last Updated :
28 Apr, 2025
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
Similar Reads
How to check if an array includes an object in JavaScript ? Check if an array includes an object in JavaScript, which refers to determining whether a specific object is present within an array. Since objects are compared by reference, various methods are used to identify if an object with matching properties exists in the array.How to check if an array inclu
3 min read
How to Check if an Object is Empty in TypeScript ? In TypeScript, it's common to encounter scenarios where you need to determine if an object is empty or not. An empty object typically means it contains no properties or all its properties are either undefined or null. Below are the methods to check if an object is empty or not in TypeScript: Table o
3 min read
Check if an Array is Empty or not in TypeScript In TypeScript, while performing any operation on the array, it is quite important that there should be elements or data in the array, else no operation can be done. We can check whether the array contains the elements or not by using the below-listed methods: Table of Content Using length PropertyUs
2 min read
How can I Define an Array of Objects in TypeScript? In TypeScript, the way of defining the arrays of objects is different from JavaScript. Because we need to explicitly type the array at the time of declaration that it will be an Array of objects. In this article, we will discuss the different methods for declaring an array of objects in TypeScript.
6 min read
How to Convert an Array of Objects into Object in TypeScript ? Converting an array of objects into a single object is a common task in JavaScript and TypeScript programming, especially when you want to restructure data for easier access. In this article, we will see how to convert an array of objects into objects in TypeScript.We are given an array of objects a
3 min read
Check if an Object is Empty in TypeScript In TypeScript, determining whether an object is empty involves checking if the object has any properties. This can be achieved through various built-in methods and loops. In this article, we will explore three different approaches to check if an object is empty in TypeScript.Table of ContentUsing Ob
2 min read
How to Check if all Enum Values Exist in an Object in TypeScript ? To check if all values of an enum exist in an object in TypeScript, iterate through the enum values and verify their presence in the object. This ensures that the object encompasses all possible enum values, confirming completeness and adherence to the enum definition. There are various methods to c
3 min read
How to Check if a Variable is an Array in JavaScript? To check if a variable is an array, we can use the JavaScript isArray() method. It is a very basic method to check a variable is an array or not. Checking if a variable is an array in JavaScript is essential for accurately handling data, as arrays have unique behaviors and methods. Using JavaScript
2 min read
How to Create a Typed Array from an Object with Keys in TypeScript? Creating a typed array from the keys of an object in TypeScript ensures that your application maintains type safety, particularly when interacting with object properties. Here we'll explore different approaches to achieve this using TypeScript's built-in Object methods.Below are the approaches used
3 min read
How to Check the Type of an Object in Typescript ? When working with TypeScript, understanding how to check the type of an object is crucial for ensuring type safety and maintaining code integrity. TypeScript, being a statically typed superset of JavaScript, provides several approaches to accomplish this task as listed below.Table of ContentUsing th
3 min read