Close
Close full mode
logoWebDevAssist

TypeScript program to add an element to an array if it does not exist

TypeScript program to add an element to an array if it does not exist:

This post will show you how to add an element to an array if it doesn't exist in the array. We will use the push method to add an element.

I will show you different ways to solve it with array of strings, numbers and objects.

Example 1: By using indexOf and push:

indexOf method can be used to find the first index of an element in an array. Again, we can use the push method to add an element to the end of an array.

By using these two methods, we can easily check if an element exists in an array or not and add it if it doesn't exist.

let strArray = Array.of('one', 'two', 'three', 'four');
if(strArray.indexOf('five') === -1){
strArray.push('five');
}else{
console.log('It is already exists in the array.');
}
console.log(strArray);

indexOf returns -1 if it doesn't find the element in the array. In the above example, we are adding the string 'five' to strArray if the return of strArray is -1, i.e. if it doesn't exist.

We can also write this program as:

let strArray = Array.of('one', 'two', 'three', 'four');
strArray.indexOf('five') === -1 ? strArray.push('five') : console.log('It is already exists in the array.')
console.log(strArray);

Example 2: Add the element to the start of the array:

We can also add an element to the start of an array. We can follow the same approach. But instead of using push, we have to use unshift. unshift adds an element to the start of an array.

So, we can rewrite the above program as like below:

let strArray = Array.of('one', 'two', 'three', 'four');
strArray.indexOf('zero') === -1 ? strArray.unshift('zero') : console.log('It is already exists in the array.')
console.log(strArray);`

Run this program and it will add 'zero' to the start of the array.

[ 'zero', 'one', 'two', 'three', 'four' ]

Example 3: Working with objects:

If you want to work with an array of objects, you need to use the findIndex method instead of indexOf.

findIndex method takes one function and it uses that function to find out the first element that satisfies it. It returns -1 if no element is found.

For example:

let strArray = Array.of({ name: 'Alex', age: 20 }, { name: 'Bob', age: 21 }, { name: 'Chandler', age: 19 });
let s = { name: 'Daisy', age: 21 };
strArray.findIndex(e => e.age === s.age && e.name === s.name) === -1 ? strArray.push(s) : console.log(`User already exists}`);
console.log(strArray);
  • strArray is an array of objects.
  • s is a object we want to add to this array if it doesn't exists.
  • By using findIndex, we are comparing the age and name of each element in the array. It returns -1 if no element is found in the array that satisfies the function.
  • If no element is found that is equal to s, it adds it to the end of the array.
  • If it finds an equal element, it prints one message.

If you run the above program, it will print:

TypeScript findIndex
TypeScript findIndex

{name: 'Daisy', age: 21} is not in the array. So, it added it to the end.

Subscribe to our Newsletter

Previous
Typescript extract a section of an array using slice
Next
Introduction to types in typescript