Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js push if not exists

// ✅ With primitives
const arr1 = ['a', 'b', 'c', 'd'];
const value1 = 'e';

if (!arr1.includes(value1)) {
  arr1.push(value1);
}

// ✅ With Objects
const arr2 = [{id: 1}, {id: 2}];
const value2 = {id: 3};

const index = arr2.findIndex(object => object.id === value2.id);

if (index === -1) {
  arr2.push(value2);
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: js method string remove extra spaces 
Javascript :: username validation in javascript 
Javascript :: select all children javascript 
Javascript :: display none using jquery 
Javascript :: remove quotes from array javascript 
Javascript :: trim comma in javascript 
Javascript :: hello world program in node js 
Javascript :: how to create round image in react native 
Javascript :: javascript count occurrences of letter in string 
Javascript :: get lines as list from file node js 
Javascript :: javascript snumber two decimal places as string 
Javascript :: change onclick attribute with javascrip 
Javascript :: horizontal divider react native 
Javascript :: jquery each has class 
Javascript :: Exceeded maximum budget Budget 10 kB was not met by 478 bytes with a total of 10.5 kB. 
Javascript :: async function fetchJson 
Javascript :: clz32 
Javascript :: react native display inline block 
Javascript :: Typography material ui import 
Javascript :: select2 disable search 
Javascript :: To set the dropdown default value using jquery 
Javascript :: add attribute selected jquery 
Javascript :: Valid Time javascript 
Javascript :: generate random boolean javascript 
Javascript :: remove div javascript 
Javascript :: byte to gb javascript 
Javascript :: javascript detect click outside of element 
Javascript :: activate es6 module node package.json 
Javascript :: how to upgrade to react 18 
Javascript :: dom is loaded 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =