Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

js add to array if not exists

var newItem = "NEW_ITEM_TO_ARRAY";
var array = ["OLD_ITEM_1", "OLD_ITEM_2"];

array.indexOf(newItem) === -1 ? array.push(newItem) : console.log("This item already exists");

console.log(array)
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #js #add #array #exists
ADD COMMENT
Topic
Name
6+2 =