GraphicRiver

jQuery check if in array

708 posts
  • Belgium
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 3-4 years
  • Sold between 100 and 1 000 dollars
Yaeko says

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!

812 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 3-4 years
iamthwee says

You could just create a nested for loop, that way you can count exactly how many times each item in your array is duplicated…

2507 posts Put a Donk On It
  • Attended a Community Meetup
  • Beta Tester
  • Bought between 10 and 49 items
  • Contributed a Tutorial to a Tuts+ Site
  • Elite Author
  • Exclusive Author
  • Has been a member for 3-4 years
  • Interviewed on the Envato Notes blog
+5 more
ThemeProvince says

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;

}

708 posts
  • Belgium
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 3-4 years
  • Sold between 100 and 1 000 dollars
Yaeko says


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!

5007 posts The Dude Abides
  • United States
  • Elite Author
  • Has been a member for 4-5 years
  • Exclusive Author
  • Sold between 50 000 and 100 000 dollars
  • Contributed a Tutorial to a Tuts+ Site
  • Author had a Free File of the Month
+4 more
CodingJack says
var ar = ["a", "a", "b", "c"];

var duplicate = ar.indexOf("a") !== ar.lastIndexOf("a");

console.log(duplicate); // true
by
by
by
by
by