interface Company {
name: string;
desc: string;
workForce: number;
const companies: Company[] = [
{ name: "GeeksforGeeks", desc: "A Computer Science Portal.", workForce: 200 },
{ name: "Company 2", desc: "Description 1", workForce: 30 },
{ name: "Company 3", desc: "Description 2", workForce: 10 },
const matchedCompany = companies.find(company => company.workForce > 30);
console.log(matchedCompany);
Output:
"name": "GeeksforGeeks",
"desc": "A Computer Science Portal.",
"workForce": 200
}
TypeScript Array find() method-FAQs
How does TypeScript handle the type of the find() return value?
TypeScript infers the return type based on the array’s type. If no element matches the condition, the return type is T | undefined, where T is the element type.
Can you use find() with objects in TypeScript?
Yes, you can use find() with an array of objects. The callback can check for a specific property value:
const users = [{ name: 'John' }, { name: 'Jane' }]
;const user = users.find(u => u.name === 'Jane');
What happens if find() doesn’t find any matching element?
If no element matches the condition, the find() method returns undefined.
Is find() available on all array types in TypeScript?
Yes, find() can be used with arrays of any type in TypeScript, whether it’s an array of strings, numbers, objects, or custom types.
Does the find() method mutate the original array?
No, the find() method does not modify the original array. It simply searches and returns the first matching element without altering the array.
Unleash the full potential of JavaScript with TypeScript. Our beginner-friendly
TypeScript tutorial
guides you through the basics and prepares you for advanced development.
How to find the Total Number of Elements in an Array in TypeScript ?
In TypeScript, arrays are a common data structure used to store collections of elements. You can store multiple elements of different or the same data type inside them by explicitly typing. The below methods can be used to accomplish this task: Table of Content Using the length property Using the forEach methodUsing the reduce methodUsing a for Loo
TypeScript Array reverse() Method
The Array.reverse() is an inbuilt TypeScript function which is used to reverses the element of an array. Syntax: array.reverse(); Parameter: This methods does not accept any parameter. Return Value: This method returns the reversed single value of the array. Below examples illustrate the Array reverse() Method in TypeScript. Example 1: C/C++ Code /
TypeScript | Array pop() Method
The Array.pop() is an inbuilt TypeScript function which is used to removes the last element from an array and returns that element. Syntax: array.pop(); Parameter: This methods does not accept any parameter. Return Value: This method returns the removed element from the array. Below examples illustrate Array pop() method in TypeScript. Example 1: C
TypeScript Array includes() Method
TypeScript includes() method allows you to determine if a specific element is present, within an array. can also determine if a particular element exists or not at a particular index position. Syntax:array.includes(searchElement[, fromIndex])Parameters:searchElement: The value to search for within the array.fromIndex (optional): The starting index
TypeScript Array Symbol.iterator Method
In TypeScript the Symbol.iterator function plays a role, in allowing iteration over arrays using for...of loops and other iterator-based mechanisms. This built-in function eliminates the need, for definition when working with arrays simplifying the process of accessing elements in a manner. Syntax:const iterator: Iterator<T> = array[Symbol.it
TypeScript Array.prototype.copyWithin() Method
The copyWithin() method is available in the TypeScript Array.prototype it helps you to rearrange the elements within the array by copying a sequence of array elements. This method is good for tasks such as shifting elements, removing them without creating gaps, or copying portions of arrays. Syntaxarray.copyWithin(target, start, end)Parameters:targ
TypeScript Array forEach() Method
The forEach() method in TypeScript calls a specified function for each element in an array. It takes a callback function executed on each element and an optional thisObject parameter to use as this context. It enhances iteration readability and efficiency. Syntaxarray.forEach(callback[, thisObject])ParametersThis method accepts two parameters as me
TypeScript Array sort() Method
The sort() method in TypeScript sorts the elements of an array and returns the sorted array. By default, it sorts an array in ascending order. It can take an optional compareFunction to define the sort order, allowing for custom sorting logic. Syntaxarray.sort( compareFunction )Parameter: This method accepts a single parameter as mentioned above an
TypeScript Array splice() Method
The Array.splice() is an inbuilt TypeScript function that changes the content of an array by adding new elements while removing old ones. It takes an index, a count of elements to remove, and optional elements to add, returning the removed elements and modifying the original array. Syntaxarray.splice(index, howMany, [element1][, ..., elementN]); Pa
Array filter() Method - TypeScript
The Array.filter() method in TypeScript creates a new array with elements that pass the test provided by a callback function. It does not modify the original array and accepts an optional thisObject for context within the callback function. Syntaxarray.filter(callback[, thisObject])Parameter: This method accepts two parameters as mentioned and desc
TypeScript Array map() Method
The Array.map() is an inbuilt TypeScript function that creates a new array with the results of calling a provided function on every element in the array. Syntaxarray.map(callback[, thisObject])Parameter: This method accepts two parameters as mentioned above and described below: callback: This parameter is the function that produces an element of th
TypeScript Array reduce() Method
The Array.reduce() method in TypeScript is used to reduce an array to a single value by applying a callback function to each element of the array. This article will provide a detailed guide on how to use the reduce() method, complete with examples to illustrate its functionality. Syntaxarray.reduce(callback[, initialValue])Parameterscallback: A fun
TypeScript Array join() Method
The Array.join() method in TypeScript is a built-in function used to join all the elements of an array into a single string. This method is particularly useful when you need to concatenate array elements with a specific separator. SyntaxHere, we will see the syntax of the join() method and understand its parameters. array.join(separator?: string):
TypeScript Array keys() Method
In TypeScript, the keys() method is not directly available on arrays in the same way it is on objects. Instead, the Object.keys() method from the Object class can be used to obtain the keys of an array. When applied to an array, Object.keys() returns an array containing the string representations of the indices. Alternatively, the Array.prototype.k
TypeScript Array Object.values() Method
Object.values() is a functionality in TypeScript that returns an array extracting the values of an object. The order of elements is the same as they are in the object. It returns an array of an object's enumerable property values. Syntax:Object.values(object);Parameters:object: Here you have to pass the object whose values you want to extract.Retur
TypeScript Array some() Method
The Array.some() method in TypeScript is used to check if at least one element in the array passes the test implemented by the provided function.Syntax: array.some(callback[, thisObject])Parameter: This method accepts two parameters as mentioned above and described below: callback: This parameter is the Function to test for each element.thisObject:
TypeScript Array.isArray() Method
In TypesScript the isArray() method returns true if the value passed as a parameter is a type of array. If the value is indeed an array the function will return true, otherwise it will return false. SyntaxArray.isArray(value: any): booleanParametersvalue: The value that will be checked for array type.Return ValueIf the given value is an array then
TypeScript Array toString() Method
The Array.toString() method in TypeScript is a built-in function used to convert an array and its elements into a string representation. This method is useful when you need a simple, comma-separated string version of your array. SyntaxThe syntax for using the toString() method is straightforward: array.toString()Parameter: This method does not acce
TypeScript Array push() Method
The Array.push() method in TypeScript is a built-in function used to append one or more elements to the end of an array. It modifies the original array and returns the new length of the array. SyntaxHere is the syntax for using the push() method: array.push(element1, ..., elementN)Parameter: This method accept a single parameter as mentioned above
TypeScript Array findIndex() Method
The findIndex() function in TypeScript helps you find the index position of the first element in an array that meets a specified condition. If no element meets the condition, it returns -1. Syntaxarray.findIndex(callbackFn(value, index, array): boolean): numberParameterscallbackFn: A function that is called for each element in the array. It takes t
TypeScript Array indexOf() Method
The Array.indexOf() method in TypeScript is used to find the index of the first occurrence of a specified element in an array. If the element is not found, it returns -1. Syntaxarray.indexOf(searchElement[, fromIndex])Parameter: This method accepts two parameters as mentioned above and described below: searchElement: This parameter is the Element t
TypeScript Array fill() Method
The fill() method in TypeScript is utilized to replace all the elements within an array with a fixed value. This action alters the array itself and returns the updated version. This method is helpful for the easy transformation of an array in TypeScript. Syntaxarray.fill(value[, start[, end]])Parametersvalue: The value you want to use for populatin
TypeScript Array.from() Method
The Array.from() method in TypeScript converts array-like or iterable objects into actual arrays. It allows the creation of arrays from objects with a length property or iterable structures, optionally applying a map function to each element during the conversion. SyntaxArray.from(object, mapFunction, thisValue)Parametersobject: This Parameter is w
TypeScript Array Object.entries() Method
The Object.entries() method in TypeScript is used for creating an array of key-value pairs from an object's keys and values. This method simplifies the process of iterating over an object's properties and enables easier data manipulation. SyntaxObject.entries(obj);Parameterobj: It takes the object as a parameter whose key-value pair you want to sto
TypeScript Array unshift() Method
The Array.unshift() method in TypeScript is an inbuilt function used to add one or more elements to the beginning of an array. It modifies the original array and returns the new length of the array. Syntaxarray.unshift( element1, ..., elementN )ParameterThis method accepts n number of similar elements. element1, ..., elementN : This parameter is th
TypeScript Array shift() Method
The Array.shift() method is an inbuilt TypeScript function used to remove the first element from an array and return that element. This operation modifies the original array by removing the first element. Syntaxarray.shift(); Parameter: This method does not accept any parameter. Return Value: This method returns the removed single value of the arra
TypeScript Array every() Method
The Array.every() method is an inbuilt TypeScript function that checks if all elements in an array pass the test implemented by the provided function. It returns true if every element satisfies the test; otherwise, it returns false. Syntax array.every(callback[, thisObject])Parameter: This method accepts two parameters as mentioned above and descri
TypeScript Array reduceRight() Method
The Array.prototype.reduceRight() method is a built-in TypeScript function used to apply a reducer function to each element of the array, processing the elements from right to left, ultimately reducing the array to a single value. Syntax: array.reduceRight(callback[, initialValue]) Parameters callback: The function to execute on each element in the
TypeScript Array flat() Method
The Array.prototype.flat() method in TypeScript allows you to create a new array with all sub-array elements concatenated into it, up to a specified depth. This method helps in flattening nested arrays to the desired depth, making it easier to handle deeply nested structures. Syntax:array.flat([depth]); Parameters:depth (optional): The depth level
TypeScript Array.prototype.at() Method
The Array.prototype.at() method in TypeScript allows you to access elements in an array using both positive and negative indices. It returns the element stored at the specified index if the index is valid, otherwise it returns undefined. Syntax:array.at(index: number): T | undefinedParameter:index: It takes the index of the element whose value it w
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our