7

I have a jQuery array:

var arr = $('input[name$="recordset"]');

I am getting the value of array like 8 or 6

If array values are repeating or duplicate I need to show "please do not repeat the values". If not I need to proceed further.

Using jQuery can anybody tell me how to find the duplicate values?

6
  • api.jquery.com/jQuery.unique might be helpful Commented Dec 3, 2010 at 14:36
  • 1
    @kumar That's not an array. It's a jQuery object. Commented Dec 3, 2010 at 14:36
  • Thanks JAndy.. Jquery.Unique is work for DOM elements. Commented Dec 3, 2010 at 14:37
  • @kumar jQuery.unique will remove duplicate DOM elements. But there's no way that you have duplicate DOM elements when you use a selector. Commented Dec 3, 2010 at 14:38
  • 1
    I think what is OP is saying is that he's calling arr.val(), and it returns a number in the input; he wants to check for duplicate input values. Commented Dec 3, 2010 at 14:39

4 Answers 4

14
var unique_values = {};
var list_of_values = [];
$('input[name$="recordset"]').
    each(function(item) { 
        if ( ! unique_values[item.value] ) {
            unique_values[item.value] = true;
            list_of_values.push(item.value);
        } else {
            // We have duplicate values!
        }
    });

What we're doing is creating a hash to list values we've already seen, and a list to store all of the unique values. For every input the selector returns we're checking to see if we've already seen the value, and if not we're adding it to our list and adding it to our hash of already-seen-values.

Sign up to request clarification or add additional context in comments.

6 Comments

@Sean I recommend this: var val = $(item).val(); and then work with val instead of item.value. The reasoning behind this: the value of a form element is not always in its value attribute - it can be the text-content of the element, also.
@Šime - I cannot say I've ever encountered that. For my information, what form elements would this apply to?
@Sean I'll go check... give me a sec.
@Sean Using item.val is safe in Chrome. But I remember having an issue in an older version of IE. Unfortunately I only have IE9 beta here, so I cannot confirm it.
@Šime -- fair enough. I'll dig into it some more. Thanks for checking!
|
2

Hope that below snippets will help if someone looks for this kind of requirement

var recordSetValues = $('input[name$="recordset"]').map(function ()    {
          return this.value;
      }).get();     
var recordSetUniqueValues = recordSetValues.filter(function (itm, i,    a) {
          return i == a.indexOf(itm);
      });
if (recordSetValues .length > recordSetUniqueValues.length)
      { alert("duplicate resource") }

Comments

1
// For every input, try to find other inputs with the same value
$('input[name$="recordset"]').each(function() {
   if ($('input[name$="recordset"][value="' + $(this).val() + '"]').size() > 1)
      alert('Duplicate: ' + $(this).val());
});

2 Comments

The API says that unique() only works for arrays of DOM elements.
Thanks Tiu Talk.. but my array is allways checking with 0 its not alowing me to check with other values.. like 1 2 3 thanks
1
$('form').submit(function(e) {

    var values = $('input[name="recordset[]"]').map(function() {
      return this.value;
    }).toArray();

    var hasDups = !values.every(function(v,i) {
      return values.indexOf(v) == i;
    });
    if(hasDups){
       // having duplicate values
       alert("please do not repeat the values");
       e.preventDefault();
    }

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.