I have a function that is supposed to add unique elements to an array:
addLanguage = (val) => {
for(let i=0; i< val.length; i++){
console.log(val[i].id)
if(this.state.createNewDeliveryData.languages.indexOf(val[i].id === -1)){
this.state.createNewDeliveryData.languages.push(val[i])
}
}
}
the argument 'val' each time gets an array with appended elements (I have a multi select box on UI from where I add languages), that's why in If loop I am trying to check if my languages array already has that specific element. However this has no effect and at the end my languages array contains same elements i.e. same element is added twice or thrice etc. on subsequent calls.
['German']
['German', 'German', 'Arabic']
...
What am I doing wrong?
uniqorunionWith.idcoming from? Looks like an array of strings to me.