Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

append to array check if exists javascript

var array = ['test1','test2'];
var item = 'test2';

if(array.indexOf(item) === -1) {
    array.push(item);
}
// array = ["test1", "test2"]

if(array.indexOf(item) !== -1) {
    array.push(item);
}
// array = ["test1", "test2", "test2"]
Comment

js push into array if item exist

if(this.items.indexOf(item) === -1) {
    this.items.push(item);
    console.log(this.items);
}
Comment

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)
Comment

js add to array if not exists

const key = 'mDisplayName'
let updatedData = [...new Map(data.map(item => [item[key], item])).values()];
updatedData.map((item)=>{
	console.log(item);
})
Comment

PREVIOUS NEXT
Code Example
Javascript :: nuxt looks for npm_modules on express 
Javascript :: js toggle class 
Javascript :: how many days old am i 
Javascript :: permutation javascript 
Javascript :: jquery ajax 500 error handling 
Javascript :: add date in javascript 
Javascript :: metamask event disconnect 
Javascript :: cypress get selected dropdown value 
Javascript :: javascript check if variable is number 
Javascript :: js escape html 
Javascript :: async in useeffect 
Javascript :: how to set cookies in node js 
Javascript :: javascript remove object property 
Javascript :: how to add alternate image in img tag in react 
Javascript :: await useeffect react 
Javascript :: js remove null from array 
Javascript :: check if an array is empty javascript 
Javascript :: moment js - to find if dates are same by day 
Javascript :: discord.js on ready 
Javascript :: javascript text word wrap replace 
Javascript :: using iframe in chrome console 
Javascript :: get date js 
Javascript :: javascript multiply array with scalar 
Javascript :: uncheck a checkbox in javascript 
Javascript :: render tab screen when goBack function is called of other screen 
Javascript :: prepend to array javascript 
Javascript :: express redirect 
Javascript :: getelementsbyclassname remove class 
Javascript :: javascript change paragraph text 
Javascript :: dinosaur game hacks 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =