Array.push() and unique items
JavascriptJavascript Problem Overview
I have a simple case of pushing unique values into array. It looks like this:
this.items = [];
add(item) {
if(this.items.indexOf(item) > -1) {
this.items.push(item);
console.log(this.items);
}
}
Seems pretty straight-forward, right? No, as it appears. It doesn't add any values. I am sure it's some kind of silly mistake on my side, but I can't seem to find it.
Javascript Solutions
Solution 1 - Javascript
Yep, it's a small mistake.
if(this.items.indexOf(item) === -1) {
this.items.push(item);
console.log(this.items);
}
Solution 2 - Javascript
You can use the Set structure from ES6 to make your code faster and more readable:
// Create Set
this.items = new Set();
add(item) {
this.items.add(item);
// Set to array
console.log([...this.items]);
}
Solution 3 - Javascript
try .includes()
[1, 2, 3].includes(2); // true
[1, 2, 3].includes(4); // false
[1, 2, 3].includes(3, 3); // false
[1, 2, 3].includes(3, -1); // true
[1, 2, NaN].includes(NaN); // true
so something like
const array = [1, 3];
if (!array.includes(2))
array.push(2);
note the browser compatibility at the bottom of the page, however.
Solution 4 - Javascript
If you use Lodash, take a look at _.union
function:
let items = [];
items = _.union([item], items)
Solution 5 - Javascript
In case if you are looking for one liner
For primitives
(this.items.indexOf(item) === -1) && this.items.push(item);
For objects
this.items.findIndex((item: ItemType) => item.var === checkValue) === -1 && this.items.push(item);
Solution 6 - Javascript
Your logic is saying, "if this item exists already, then add it." It should be the opposite of that.
Change it to...
if (this.items.indexOf(item) == -1) {
this.items.push(item);
}
Solution 7 - Javascript
I guess ES6 has set data structure, which you can use for unique entries
Solution 8 - Javascript
Using Set
this.items = new Set();
this.items.add(1);
this.items.add(2);
this.items.add(1);
this.items.add(2);
console.log(Array.from(this.items)); // [1, 2]
Solution 9 - Javascript
You have to use === -1, if it equals to -1 i.e. item is not available in your array:
this.items = [];
add(item) {
if(this.items.indexOf(item) === -1) {
this.items.push(item);
console.log(this.items);
}
}
Solution 10 - Javascript
var helper = {};
for(var i = 0; i < data.length; i++){
helper[data[i]] = 1; // Fill object
}
var result = Object.keys(helper); // Unique items
Solution 11 - Javascript
Push always unique value in array
ab = [
{"id":"1","val":"value1"},
{"id":"2","val":"value2"},
{"id":"3","val":"value3"}
];
var clickId = [];
var list = JSON.parse(ab);
$.each(list, function(index, value){
if(clickId.indexOf(value.id) < 0){
clickId.push(value.id);
}
});
Solution 12 - Javascript
so not sure if this answers your question but the indexOf the items you are adding keep returning -1. Not to familiar with js but it appears the items do that because they are not in the array yet. I made a jsfiddle of a little modified code for you.
this.items = [];
add(1);
add(2);
add(3);
document.write("added items to array");
document.write("<br>");
function add(item) {
//document.write(this.items.indexOf(item));
if(this.items.indexOf(item) <= -1) {
this.items.push(item);
//document.write("Hello World!");
}
}
document.write("array is : " + this.items);
Solution 13 - Javascript
Simple use new Set() with concat. The most performant mostly and updated:
console.log([...new Set(["hey", "we", "have", "array"].concat(["hey", "we", "add", "these", "too", "but without second hey, we, have :)"]))])
If you want to add elements in a case insensitive way, please do this for arrays;i.e:
["hey", "we", "HaVe", "ArrAy"].filter(r => r !== '').map(r => r.toUpperCase())
This will check whether empty element:
.filter(r => r !== '')
Will check case insensitive:
.map(r => r.toUpperCase()