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
🌐
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.
🌐
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.
🌐
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 ...
🌐
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 }
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › includes
Array.prototype.includes() - JavaScript | MDN
The includes() method of Array instances determines whether an array includes a certain value among its entries, returning true or false as appropriate.
🌐
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) // ...
🌐
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
// Requiring the lodash library ... }]; let obj = { "geeks1": 10, "geeks2": 12 } // Check value is found // or not by _.includes() method console.log(_.find(arr, obj));...
Published   July 11, 2025
Find elsewhere
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › some
Array.prototype.some() - JavaScript | MDN
The some() method of Array instances returns true if it finds an element in the array that satisfies the provided testing function. Otherwise, it returns false.
🌐
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
This shallowEqualityCheck function first checks if the objects have the same number of properties and, if they do, it checks that the value of each property in object 1 is the same as the value of the corresponding property in object 2.
🌐
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 - In JavaScript, you can find if an object exists in an array of objects using different methods.
🌐
CoreUI
coreui.io › answers › how-to-check-if-an-array-contains-a-value-in-javascript
How to check if an array contains a value in JavaScript · CoreUI
September 18, 2025 - This is the same approach we use in CoreUI components for permission checks and feature detection. For object arrays, use some() with a custom comparison: users.some(user => user.id === targetId).
🌐
Stack Abuse
stackabuse.com › javascript-check-if-array-contains-a-value-element
JavaScript: Check if Array Contains a Value/Element
March 8, 2023 - When searching for an object, includes() checks whether the provided object reference matches the one in the array.
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-check-if-a-value-exists-in-an-array-in-javascript.php
How to Check If a Value Exists in an Array in JavaScript
<script> var fruits = ["Apple", ... exists!") } else{ alert("Value does not exists!") } </script> ES6 has introduced the includes() method to perform this task very easily....
🌐
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 ...
🌐
W3Schools
w3schools.com › jsref › jsref_includes_array.asp
JavaScript Array includes() Method
abort afterprint animationend animationiteration animationstart beforeprint beforeunload blur canplay canplaythrough change click contextmenu copy cut dblclick drag dragend dragenter dragleave dragover dragstart drop durationchange ended error focus focusin focusout fullscreenchange fullscreenerror hashchange input invalid keydown keypress keyup load loadeddata loadedmetadata loadstart message mousedown mouseenter mouseleave mousemove mouseover mouseout mouseup offline online open pagehide pageshow paste pause play playing progress ratechange resize reset scroll search seeked seeking select show stalled submit suspend timeupdate toggle touchcancel touchend touchmove touchstart transitionend unload volumechange waiting wheel HTML Event Objects
🌐
Futurestud.io
futurestud.io › tutorials › check-if-an-array-contains-a-given-value-in-javascript-or-node-js
Check If an Array Contains a Given Value in JavaScript or Node.js
Depending on the array you’re running the check on, you may choose one of the array methods array.find(predicate) or array.includes(value) to detect whether a specific value exists.
🌐
Designcise
designcise.com › web › tutorial › how-to-check-if-an-object-property-value-exists-in-a-javascript-array-of-objects
JS: Check If Object Property Value Exists in Array of Objects - Designcise
December 13, 2020 - We can use this to test if a key in the object of arrays has a certain value in the following way: // ES5+ console.log(objs.some((obj) => obj.name === 'John')); // output: true · In ES6+, we can destructure function arguments to simplify the syntax even more. For example: // ES6+ console.log(objs.some(({ name }) => name === 'John')); // output: true · If you're unable to use the latest features of JavaScript, then perhaps you can rely on a simple for loop to check if a key in the object of arrays has a certain value.