js add to array if not exists

Solutions on MaxInterview for js add to array if not exists by the best coders in the world

showing results for - "js add to array if not exists"
Violeta
17 Oct 2016
1if(this.items.indexOf(item) === -1) {
2    this.items.push(item);
3    console.log(this.items);
4}
5
Jariel
29 May 2016
1var array = ['test1','test2'];
2var item = 'test2';
3
4if(array.indexOf(item) === -1) {
5    array.push(item);
6}
7// array = ["test1", "test2"]
8
9if(array.indexOf(item) !== -1) {
10    array.push(item);
11}
12// array = ["test1", "test2", "test2"]
Djibril
17 Mar 2020
1var newItem = "NEW_ITEM_TO_ARRAY";
2var array = ["OLD_ITEM_1", "OLD_ITEM_2"];
3
4array.indexOf(newItem) === -1 ? array.push(newItem) : console.log("This item already exists");
5
6console.log(array)
queries leading to this page
add to array if exists javascriptpush element in array if not exist jsjavascript push if not in arrayjava script check array if not exist pushjavascript add item to array if not exists remove if existsjavascript array push if not existjavascript array check for value if not present push it to arrayadd only if doesn exist to array jsarray add if not exist javascriptjs add to array if number not existsarray push if value existspush element in array if not exist js es6append to array check if exists javascriptjs push into array if not exists remove if existsadd number to array if doesn 27t exist jstypescript add to array if not existsadd item in array js if not existsjavascript add item if not in arrayadd to array if not existjs add if not in arrayjs if value already in array then not pushadd to array if doesn 27t exist javascriptjavascript push element in array if not existscheck if value exists in array else add javascriptadd to array if not exists javascriptpush to array if not exist javascripthow to not add elements in array if id exists javascriptadd item to array if it doesn 27t exist javascriptjavascript add to array if not presentjs push if not in arrayjavascript find in array push if does not existjavascript check if element exist in array before pushjavascript push to array if not existjs push into array if not existsjavascript add to array if not already thereadd item to array if it doesn 27t exist js add element to array if not exists javascriptif does not exist array 2c add jsjs push to array if not existsjs add to array if not existsjavascript add to array if existjs array push if not existsadd value to array if not exists javascriptjs add element to array if not existcheck if item exist in array and push itjavascript add to array if not existjs push if is not in arrayjavascript add to array if it doesn 27t existjavascript push item to array if its not existsjavascript push into array if valuehow to add item in array if not exists javascripthow to not add item in array if exists javascriptjs array push element if not existadd item to array if it doesn 27t exist else remove it javascriptjavascript push to array if not existsjs check if exists in array before pushjavascript array add value if doesn 27t existjs add to list if not existjavascript add to array if not foundjavascript add value to array if not existsjavascript add element to array if not existsarray push if not exists javascriptjs check array if item exists then addpush in array if id not exist jsjs add to array if not exists