Reputation: 3562
I have an array that I have created that I want to check to ensure that it does not contain duplicate values.
My approach to this was to create a second array (tempArray) take the value from the first array (uniqueLabel) and test that value against all items within tempArray. If the value is not detected insert it. Otherwise alert the user
var tempArray = []; //temp array to store names
$.each(self.producNames().SelectedAttributes(), function (index, value) {
var uniqueLabel = value.LabelName().toLowerCase();
//I did have the evaluation set to !($.inArray(value.LabelName().toLowerCase(), tempArray, 0)
//however this alwasy returned false I set it to true just to see what would happen
//now it always returns true
if ($.inArray(value.LabelName().toLowerCase(), tempArray, 0)) {
tempArray.push(value.LabelName());
console.log(value.LabelName() + " added to array[]");
}
else {
console.log("label " + uniqueLabel + " already exists at index "+ tempArray.indexOf());
}
I'm not that familiar with the jQuery inArray()
function but in looking at the documentation it looks to be correct how I have it setup. However something is not correct because when I pass in duplicate labelnames they are inserted into the array where I would expect the operation to return a 0 and execute the else block. Instead it always returns true.
Any suggestions on what I have incorrect here?
thanks
Upvotes: 2
Views: 605
Reputation: 3859
This may be as simple as using jQuery.unique().
From their docs:
"The $.unique()
function searches through an array of objects, sorting the array, and removing any duplicate nodes. A node is considered a duplicate if it is the exact same node as one already in the array; two different nodes with identical attributes are not considered to be duplicates."
The docs go on to say that it only works on arrays of DOM elements, however I just tested it with strings and it worked just fine.
$.unique(['1', '2', '2', '3', '1']);
//=> ['3', '2', '1']
Hope that helps.
Upvotes: 0
Reputation: 780818
$.inArray()
doesn't return a boolean. It returns the index of the matching item, or -1
if it's not found. If you try to use it as a boolean, it will be false when the match is at index 0, and true if it's not found or at some other index, which is obviously not what you want.
You want:
var pos = $.inArray(uniqueLabel, tempArray);
if (pos == -1) {
tempArray.push(uniqueLabel);
console.log(uniqueLabel + " added to array[]");
} else {
console.log("label " + uniqueLabel + " already exists at index "+ pos);
}
Upvotes: 3
Reputation: 27012
$.inArray()
returns the index of the item in the array, or -1
if the item isn't found.
So your if
statement will always return true, unless the item you're searching for is the first item in the array, in which case the return value is 0
, which evaluates to false.
Try something like this instead:
if ($.inArray(value.LabelName().toLowerCase(), tempArray, 0) > -1) {
//found it
} else {
// not there
}
Something like this might be more efficient (and easier to read, in my opinion):
var tempArray = [];
$.each(self.productNames().SelectedAttributes(), function(index, value) {
if (tempArray[value.LabelName()]) {
//found it
} else {
tempArray[value.LabelName()] = 1;
}
});
Upvotes: 2