Hi,
I have an array:
["Test page 03", "Test page 03", Test page 04]
I would like to check if “Test Page 03” is present several times in the array.
Can’t use "unique()" function of jQuery since it remove duplicates, I don’t want to remove it, I just want to check it to pass an if statement.
"jQuery.inArray()" isn’t really doing what I’m looking for.
Anyone an idea?
Thanks!
You could just create a nested for loop, that way you can count exactly how many times each item in your array is duplicated…
- Attended a Community Meetup
- Author was Featured
- Beta Tester
- Bought between 10 and 49 items
- Contributed a Tutorial to a Tuts+ Site
- Exclusive Author
- Has been a member for 3-4 years
- Interviewed on the Envato Notes blog
Yaeko said
Hi,I have an array:
["Test page 03", "Test page 03", Test page 04]I would like to check if “Test Page 03” is present several times in the array. Can’t use
"unique()"function of jQuery since it remove duplicates, I don’t want to remove it, I just want to check it to pass an if statement."jQuery.inArray()"isn’t really doing what I’m looking for.Anyone an idea?
Thanks!
var present = false;
var present_count = 0;
for(var i in array)
{
if (array[i] == "Test Page 03")
{
present_count += 1;
}
}
if (present_count > 1)
{
present = true;
}
ThemeProvince said
Yaeko said
Hi,I have an array:
["Test page 03", "Test page 03", Test page 04]I would like to check if “Test Page 03” is present several times in the array. Can’t use
"unique()"function of jQuery since it remove duplicates, I don’t want to remove it, I just want to check it to pass an if statement."jQuery.inArray()"isn’t really doing what I’m looking for.Anyone an idea?
Thanks!var present = false; var present_count = 0; for(var i in array) { if (array[i] == "Test Page 03") { present_count += 1; } } if (present_count > 1) { present = true; }
Okay, it’s working in this kind of way. Thanks!
- United States
- Has been a member for 4-5 years
- Exclusive Author
- Author was Featured
- Sold between 50 000 and 100 000 dollars
- Item was Featured
- Contributed a Tutorial to a Tuts+ Site
- Author had a Free File of the Month
var ar = ["a", "a", "b", "c"];
var duplicate = ar.indexOf("a") !== ar.lastIndexOf("a");
console.log(duplicate); // true
