Skip to content
the poor coder

find document which is not in array

Poor Man
Published
Reading time
2 min read

Finding a document which is not in an array can be achieved in various ways. It all depends on the context of the array and the document you are attempting to find.

If you know the format of the document you are looking for, then you can use the Array.prototype.includes() method. This method will search through the array and return a boolean value indicating whether or not the document is present. For example:

Text
const array = ['doc1', 'doc2', 'doc3'];
const document = 'doc4';

if (array.includes(document)) {
    console.log('Document found!');
} else {
    console.log('Document not found.');
}

If you don't know the format of the document you are looking for, but know that it should not be present in the array, then you can use the Array.prototype.indexOf() method. This method searches the array and returns the index of the document if it is present. If the document is not present, it will return a -1. For example:

Text
const array = ['doc1', 'doc2', 'doc3'];
const document = 'doc4';

if (array.indexOf(document) !== -1) {
    console.log('Document found!');
} else {
    console.log('Document not found.');
}

Finally, if you are working with objects in the array, you can use the Array.prototype.find() method. This method searches the array for an element that satisfies the provided condition. Once found, it will return the element. If no element satisfying the condition is found, it will return undefined. For example:

Text
const array = [{name: 'doc1'}, {name: 'doc2'}, {name: 'doc3'}];
const document = {name: 'doc4'};

const found = array.find(el => el.name === document.name);

if (found) {
    console.log('Document found!');
} else {
    console.log('Document not found.');
}

These are just a few of the ways you can find a document which is not in an array. It all depends on the context of the array and the document you are attempting to find. I hope this helps!

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