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 8313779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:36:30+00:00 2026-06-08T20:36:30+00:00

While trying to write some awesome JS I ran into some strange JS-behaviour. I’ve

  • 0

While trying to write some awesome JS I ran into some strange JS-behaviour. I’ve created a piece of code to demonstrate my problem:

http://jsfiddle.net/CeyCy/

<html>
  <head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript">
    $(function() {
      var images = $('.images').children();
      var filtered = images.filter(function(i, image) {
        console.log(this);
      }, "abc");
    });
    </script>
  </head>
  <body>
    <div class="options">
      <div class="filters">
      </div>
      <ol class="pagination">
      </ol>
    </div>
    <div class="results">
      <ul class="images">
        <li data-category="Zakelijk">Result 1</li>
        <li data-category="Speels">Result 2</li>
        <li data-category="Blogging">Result 3</li>
        <li data-category="Zakelijk">Result 4</li>
        <li data-category="Speels">Result 5</li>
        <li data-category="Blogging">Result 6</li>
        <li data-category="Zakelijk">Result 7</li>
        <li data-category="Speels">Result 8</li>
        <li data-category="Blogging">Result 9</li>
        <li data-category="Zakelijk">Result 10</li>
        <li data-category="Speels">Result 11</li>
        <li data-category="Blogging">Result 12</li>
        <li data-category="Zakelijk">Result 13</li>
        <li data-category="Speels">Result 14</li>
        <li data-category="Blogging">Result 15</li>
      </ul>
    </div>
  </body>
</html>

Ok, now my question. When using the Array.filter method you pass in a callback function as first parameter and an optional context as second parameter. As you can see, I’m passing “abc” as context (which is ridiculous, ofcourse). I would expect my console to throw a lot of “abc”, but instead it prints the jQuery elements!

Can someone shine some light on this please?

Thanks,

Martijn

  • 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-08T20:36:34+00:00Added an answer on June 8, 2026 at 8:36 pm

    As you can see, I’m passing “abc” as context (which is ridiculous, ofcourse). I would expect my console to throw a lot of “abc”, but instead it prints the jQuery elements!

    You’re using jQuery’s filter, which doesn’t have a context parameter, rather than ES5’s Array#filter, because you’re calling filter on a jQuery object, and jQuery objects are not arrays (although they offer a lot of array-like functionality, and several “methods” with similar names — including filter).

    Three options for you:

    makeArray

    You can use jQuery’s makeArray to turn the jQuery object into an array, e.g.:

    $(function() {
      var images = $.makeArray($('.images').children());
      var filtered = images.filter(function(i, image) {
        console.log(this);
      }, "abc");
    });
    

    Updated fiddle

    proxy / bind

    Or alternately, you can use jQuery’s proxy function (or ES5’s Function#bind) to bind the iterator function you’re passing to jQuery’s filter so it ignores the this jQuery supplies:

    $(function() {
      var images = $('.images').children();
      var filtered = images.filter($.proxy(function(i, image) {
        console.log(this);
      }, "abc"));
    });
    

    Apply Array#filter to the jQuery object

    Or you can apply Array#filter directly to the jQuery object:

    $(function() {
      var images = $('.images').children();
      var filtered = Array.prototype.filter.call(images, function(i, image) {
        console.log(this);
      }, "abc");
    });
    

    …since Array#filter is expressly defined to work on anything array-like, not just Arrays. That last might actually be the most efficient way to do it. Fiddle

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've run into some problems while trying to write a smallshell in c. The
I'm trying to write some code which replaces old content with a placeholder while
I just ran into an issue while trying to write an bitmap-manipulating algo for
I'm trying to write some html in the Html tab of TinyMCE while editing
I have come up to issues while I'm trying to write some classes, here
I am trying to write some data from an NSMutableArray to a plist, while
I'm trying to write some code to calculate a cumulative distribution function in matlab.
I am trying to write some code that works on both Linux and Win32.
I'm trying to write some microcontroller code using Texas Instruments examples, and it uses
I am a programmer, new to jsp, trying to write some code. i have

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.