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 Top answer 1 of 16
438
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'));
2 of 16
78
This small snippets works for me..
const arrayOfObject = [{ id: 1, name: 'john' }, {id: 2, name: 'max'}];
const checkUsername = obj => obj.name === 'max';
console.log(arrayOfObject.some(checkUsername))
if you have array of elements like ['john','marsh'] then we can do some thing like this
const checkUsername = element => element == 'john';
console.log(arrayOfObject.some(checkUsername))
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.
Videos
00:56
Find If A Value Exists In Array - JavaScript - YouTube
03:07
How To Check If Value Exists In Javascript Object - YouTube
JavaScript tips — Find if an array contains a value using Array ...
01:44
Javascript - Array - How to check if array contains all objects ...
01:07
How to check if a JavaScript object is an array? - YouTube
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.
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.
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
Top answer 1 of 15
6
function countArray(originalArray) {
var compressed = [];
// make a copy of the input array
var copyArray = originalArray.slice(0);
// first loop goes over every element
for (var i = 0; i < originalArray.length; i++) {
var count = 0;
// loop over every element in the copy and see if it's the same
for (var w = 0; w < copyArray.length; w++) {
if (originalArray[i] == copyArray[w]) {
// increase amount of times duplicate is found
count++;
// sets item to undefined
delete copyArray[w];
}
}
if (count > 0) {
var a = new Object();
a.value = originalArray[i];
a.count = count;
compressed.push(a);
}
}
return compressed;
};
// It should go something like this:
var testArray = new Array("dog", "dog", "cat", "buffalo", "wolf", "cat", "tiger", "cat");
var newArray = countArray(testArray);
console.log(newArray);
2 of 15
6
use Array.prototype.includes for example:
const fruits = ['coconut', 'banana', 'apple']
const doesFruitsHaveCoconut = fruits.includes('coconut')// true
console.log(doesFruitsHaveCoconut)
maybe read this documentation from MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
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 ...
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....
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.
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.
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
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.