I've assumed that ids are meant to be unique here. some is a great function for checking the existence of things in arrays:

const arr = [{ id: 1, username: 'fred' }, { id: 2, username: 'bill' }, { id: 3, username: 'ted' }];

function add(arr, name) {
  const { length } = arr;
  const id = length + 1;
  const found = arr.some(el => el.username === name);
  if (!found) arr.push({ id, username: name });
  return arr;
}

console.log(add(arr, 'ted'));

Answer from Andy on Stack Overflow
๐ŸŒ
Catalin's Tech
catalins.tech โ€บ array-of-objects-contains-a-value-in-javascript
Check if an Array of Objects Contains a Value in JavaScript
October 8, 2022 - This article teaches you how to use the method Array.prototype.some() to find if an array of objects contains a value in JavaScript.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Array โ€บ find
Array.prototype.find() - JavaScript | MDN
The find() method of Array instances returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Array โ€บ includes
Array.prototype.includes() - JavaScript | MDN
Values of zero are all considered to be equal, regardless of sign. (That is, -0 is equal to 0), but false is not considered to be the same as 0. NaN can be correctly searched for. When used on sparse arrays, the includes() method iterates empty slots as if they have the value undefined.
๐ŸŒ
Suraj Sharma
surajsharma.net โ€บ blog โ€บ check-object-in-javascript-array
How to check if a value exists in an Array of Objects in JavaScript | Suraj Sharma
You can use the some() method to check if an object is in the array. users.some(function (u) { if (u.username === user.username) return true; return false; }) // false // using an arrow function users.some(u=>u.username === user.username) // ...
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ javascript โ€บ how do i check if an array includes a value in javascript?
How do I check if an array includes a value in JavaScript? | Sentry
It does not check for nested object properties. If you need to compare objects with nested properties, youโ€™ll need to do a deep equality check. This shallow equality check function can be used with the some() array method to check if an array contains a specific object:
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ javascript-check-if-array-contains-a-value-element
JavaScript: Check if Array Contains a Value/Element
March 8, 2023 - We've covered the includes() function, which returns a boolean value if the value is present. The indexOf() function returns the index of a value if it's present, and -1 if it isn't. Finally, for objects, the some() function helps us search for object presence based on their contents.
๐ŸŒ
Tutorial Republic
tutorialrepublic.com โ€บ faq โ€บ how-to-check-if-an-array-includes-an-object-in-javascript.php
How to Check If an Array Includes an Object in JavaScript
<script> // An array of objects var persons = [{name: "Harry"}, {name: "Alice"}, {name: "Peter"}]; // Find if the array contains an object by comparing the property value if(persons.some(person => person.name === "Peter")){ alert("Object found ...
Find elsewhere
๐ŸŒ
W3Schools
w3schools.com โ€บ jsref โ€บ jsref_includes_array.asp
JavaScript Array includes() Method
The includes() method returns true if an array contains a specified value. The includes() method returns false if the value is not found. The includes() method is case sensitive. ... If you want to use W3Schools services as an educational ...
๐ŸŒ
freeCodeCamp
forum.freecodecamp.org โ€บ javascript
How to check if items exists in array before save it? - JavaScript - The freeCodeCamp Forum
February 27, 2019 - Hi, I am sutcked with a piece of code that I canโ€™t make it works: I have this array of objects: selectedItem[ {title: "Innovation", id: "3443klk4-4lkl34kl"}, {title: "Sales", id: "fffffs3-4lkl34kl"} ] Each object represent a tag. In the interface the user can click on a tag which is saved ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-check-if-an-array-includes-an-object-in-javascript
How to check if an array includes an object in JavaScript ? - GeeksforGeeks
It returns true because an object is present in the array. ... let arr = ["geeks1", "geeks2", "geeks3", { 1: "geeks4", 2: "geeks5" }]; let boolVar = arr.some( value => { return typeof value == "object" }); console.log(boolVar);
Published ย  July 11, 2025
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Array โ€บ some
Array.prototype.some() - JavaScript | MDN
To mimic the function of the includes() method, this custom function returns true if the element exists in the array: ... const fruits = ["apple", "banana", "mango", "guava"]; function checkAvailability(arr, val) { return arr.some((arrVal) => val === arrVal); } checkAvailability(fruits, ...
๐ŸŒ
Savvy
savvy.co.il โ€บ blog โ€บ javascript complete guide โ€บ check if a value exists in an array using js or jquery
Check if a Value exists in an Array using JS or jQuery | Savvy
August 12, 2023 - Use Array.find() to get the matching object, or Array.some() to get a boolean result. For example: users.find(u => u.name === 'Bob') returns the object if found, or undefined if not.
๐ŸŒ
EnableGeek
enablegeek.com โ€บ home โ€บ javascript check if value exists in array of objects
Javascript Check if Value Exists in Array of Objects - EnableGeek
August 7, 2023 - The some() method returns a Boolean value indicating whether at least one element in the array satisfies the provided testing function. You can use it to check if a value exists in an array of objects by defining a testing function that checks ...
๐ŸŒ
Flexiple
flexiple.com โ€บ javascript โ€บ check-value-exists-array-javascript
How to check if value exists in an array using Javascript? - Flexiple
Using a for loop the function compares each element of the array with the input value you wanted to check for. If it finds a match, the function breaks and the variable status is set to Exist, else it is set to Not Exist.
๐ŸŒ
SamanthaMing
samanthaming.com โ€บ tidbits โ€บ 81-how-to-check-if-array-includes-a-value
How to check if array includes a value in JavaScript? | SamanthaMing.com
const object = { kiwi: '๐Ÿฅ', pear: '๐Ÿ', cheese: '๐Ÿง€' },; 'kiwi' in object; // true // Use it as a conditional if ('kiwi' in object) { // do something if property key exists } @pixelfixer: And if you want the value returned, you can use Array.find()
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-check-an-array-contains-an-object-of-attribute-with-given-value-in-javascript
How to check an Array Contains an Object of Attribute with Given Value in JavaScript ? - GeeksforGeeks
July 23, 2025 - The Javascript array.some() method checks whether at least one of the elements of the array satisfies the condition checked by the argument method. In this approach, we use some() method to find a food item in the food menu array if a searched ...