Skip to main content
Format the post
Source Link
Nisal Edu
  • 7.6k
  • 4
  • 29
  • 37

For an array of strings (but not an array of objects), you can check if an item exists by calling .indexOf() and if it doesn't then just push the item into the array:

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");


   

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)

For an array of strings (but not an array of objects), you can check if an item exists by calling .indexOf() and if it doesn't then just push the item into the array:

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");


   

For an array of strings (but not an array of objects), you can check if an item exists by calling .indexOf() and if it doesn't then just push the item into the array:

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)

see comments
Source Link
user6269864
user6269864

CheckFor an array of strings (but not an array of objects), you can check if an item existexists by calling .indexOf() and if it doesn't then just push the item into the array:

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");


   

Check if item exist by calling .indexOf() and if it doesn't then just push the item into the array:

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");


   

For an array of strings (but not an array of objects), you can check if an item exists by calling .indexOf() and if it doesn't then just push the item into the array:

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");


   
deleted 13 characters in body
Source Link
Donald Duck
  • 8.8k
  • 23
  • 79
  • 101

Check if item exist by calling .indexOf().indexOf() and if it doesn't then just push the item into the array:

    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");


   

Check if item exist by calling .indexOf() and if it doesn't then just push the item into the array

    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");


   

Check if item exist by calling .indexOf() and if it doesn't then just push the item into the array:

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");


   
Loading
added 13 characters in body
Source Link
Jiří Zahálka
  • 8.2k
  • 2
  • 22
  • 17
Loading
Source Link
Jiří Zahálka
  • 8.2k
  • 2
  • 22
  • 17
Loading