Skip to content
the poor coder

js push into array if item exist

Poor Man
Published
Reading time
2 min read
On this page

JavaScript: Pushing into an array if an item exists#

As a web developer, I often come across situations where I need to push an element into an array only if it doesn't already exist. There are several ways to do this in JavaScript, but in this blog post, I will demonstrate how to achieve this using the push() method.

The push() method#

The push() method is a built-in function in JavaScript that adds one or more elements to the end of an array and returns the new length of the array. Here's a simple example:

Text
let fruits = ['apple', 'banana'];
fruits.push('orange');
console.log(fruits); // Output: ['apple', 'banana', 'orange']

In this example, we created an array of fruits and added a new element 'orange' to the end of the array using the push() method.

Pushing a unique element into the array#

If we want to push an element into an array only if it doesn't already exist, we can use the indexOf() method to check if the element exists in the array or not. Here's an example:

Text
let fruits = ['apple', 'banana'];
let newFruit = 'orange';
if (fruits.indexOf(newFruit) === -1) {
  fruits.push(newFruit);
}
console.log(fruits); // Output: ['apple', 'banana', 'orange']

In this example, we first checked if the element 'orange' exists in the array using the indexOf() method. If it returns -1, it means the element doesn't exist in the array, so we pushed the 'orange' element into the array using the push() method.

Using includes() method instead of indexOf()#

The includes() method is another way to check if an element exists in an array or not. Here's how we can modify the previous example to use the includes() method:

Text
let fruits = ['apple', 'banana'];
let newFruit = 'orange';
if (!fruits.includes(newFruit)) {
  fruits.push(newFruit);
}
console.log(fruits); // Output: ['apple', 'banana', 'orange']

This code works similarly to the previous example. We first checked if the element 'orange' exists in the array using the includes() method. If it returns false, it means the element doesn't exist in the array, so we pushed the 'orange' element into the array using the push() method.

Conclusion#

In this blog post, we learned how to push an element into an array only if it doesn't already exist using the push() method and the indexOf() or includes() methods. Using these methods, we can easily add unique elements to an array without duplicates.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC