Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this code using [].filter.call do?

I’m learning javascript and trying to write code that sorts a list, removing elements if they meet certain criteria.

I found this snippet that seems promising but don't have a clue how it works so I can adapt it to my needs:

list = document.getElementById("raffles-list").children; // Get a list of all open raffles on page
list = [].filter.call(list, function(j) {
    if (j.getAttribute("style") === "") {
        return true;
    } else {
        return false;
    }
});

Can you guys help me learn by explaining what this code block does?

like image 823
Taazar Avatar asked Jun 25 '16 21:06

Taazar


People also ask

What does the filter () method do when you use it?

The filter() method creates a new array filled with elements that pass a test provided by a function. The filter() method does not execute the function for empty elements. The filter() method does not change the original array.

What is array prototype filter call?

prototype. filter() The filter() method creates a new array with all elements that pass the test implemented by the provided function.

Which function filters the value of an array?

The JavaScript filter array function is used to filter an array based on specified criteria. After filtering it returns an array with the values that pass the filter. The JavaScript filter function iterates over the existing values in an array and returns the values that pass.

How do you filter an array of objects in react?

To filter an array of objects in React:Call the filter() method on the array. On each iteration, check if a certain condition is met. The Array. filter methods returns an array with all elements that satisfy the condition.


2 Answers

It's getting all the children of the "raffles-list" element, then returning a filtered list of those that contain an empty "style" attribute.

The first line is pretty self-evident - it just retrieves the children from the element with id "raffles-list".

The second line is a little more complicated; it's taking advantage of two things: that [], which is an empty array, is really just an object with various methods/properties on it, and that the logic on the right hand side of the equals sign needs to be evaluated before "list" gets the new value.

  1. Uses a blank array in order to call the "filter" method
  2. Tells the filter to use list as the array to filter, and uses function(j) to do the filtering, where j is the item in the list being tested
  3. If the item has a style attribute that is empty, i.e. has no style applied, it returns true.

Edit: As per OP comment, [].filter is a prototype, so essentially an object which has various properties just like everything else. In this case filter is a method - see here. Normally you just specify an anonymous function/method that does the testing, however the author here has used the .call in order to specify an arbitrary object to do the testing on. It appears this is already built into the standard filter method, so I don't know why they did it this way.

like image 186
jropella Avatar answered Nov 15 '22 07:11

jropella


Array like objects are some of javascript objects which are similar to arrays but with differences for example they don't implement array prototypes. If you want to achieve benefits of array over them (for example like question filter children of an element )you can do it this way:

Array.prototype.functionName.call(arrayLikeObject, [arg1, [arg2 ...]]);

Here in question array like is html element collection; and it takes items without any styling.

like image 39
Morteza Tourani Avatar answered Nov 15 '22 07:11

Morteza Tourani