Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9160465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:44:46+00:00 2026-06-17T13:44:46+00:00

Answering How to check if value exist in jQuery .data() function (Please no downvotes

  • 0

Answering How to check if value exist in jQuery .data() function

(Please no downvotes on the validity of adding a bunch of data values to a form object, not my decision)

I created a jsfiddle using each

which gives

Object {v1: "value11", v2: "value2", v3: "value3", v4: "value4", v5: "value5"} fiddle.jshell.net:25
Uncaught TypeError: Object #<Object> has no method 'each'

NOTE: If I wrap the object in $(..) it does not work either

SOLVED USING $.each jsfiddle using $.each

$(function() {
  $("form[name=update]").data("values", {"v1": "value11", "v2": "value2", "v3": "value3", "v4": "value4", "v5": "value5"});
  $(".but").on("click",function(e) {
    var $fData = $("form[name=update]").data("values");
    var val = $(this).val();
      console.log($fData);
    $fData.each(function(n,i) { 
        console.log("!!!",n,i)
      if (n[i]===val) {
        alert("duplicate value");
        return false;
      }    
    })  
    e.preventDefault()  
  });
});  

and

jsfiddle using grep which fails completely at the task.

$(function() {
  $("form[name=update]").data("values", {"v1": "value11", "v2": "value2", "v3": "value3", "v4": "value4", "v5": "value5"});
  $(".but").on("click",function() {
    var $fData = $("form[name=update]").data("values");
    var val = $(this).val();
    var idx = $.grep($fData, function(n,i) { 
        console.log("n",n); // why no console output for this???
        return n[i]===val; 
    });  
    console.log(idx);  
    if (idx!=-1) {
      alert("duplicate value")
    }
  });
});  

Questions:

  1. why can’t I run each on the returned object
  2. what is up with the grep I am doing (only used grep once before)
  3. why is there no console output from inside the grep
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:44:48+00:00Added an answer on June 17, 2026 at 1:44 pm

    $.each is only available to jQuery objects, so you need to wrap your object:

    $($fData).each(function (n, i) {
    

    Or pass it to $.each:

    $.each($fdata, function(n, i) {
    

    As for $.grep(), the problem is that your input is an object, not an array. $.grep sort of silently fails on you:

    grep: function( elems, callback, inv ) {
        var retVal,
            ret = [],
            i = 0,
            length = elems.length;  // <-- Here's why
        inv = !!inv;
    
        // Go through the array, only saving the items
        // that pass the validator function
        for ( ; i < length; i++ ) {
            retVal = !!callback( elems[ i ], i );
            if ( inv !== retVal ) {
                ret.push( elems[ i ] );
            }
        }
    
        return ret;
    },
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Answering another question about how const string data was stored in an executable a
Before answering, please understand I do NOT want you to do the work for
After answering to that SO question and being donwvoted I'd like to check something
While answering another question , I ended up creating a sortkey function which modified
Before answering, please note that this is a client side issue and not a
When I define a Django form class similar to this: def class MyForm(forms.Form): check
Answering another thread (see stackoverflow: generate css color schemes ) I bumped into the
When answering Git pull hook script which executes locally , I stumbled upon the
In answering another question, someone showed me the following tutorial, in which the author
Before answering, it is not as easy question as you might have thought about

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.