W
W
wufapexef2018-03-27 17:58:20
JavaScript
wufapexef, 2018-03-27 17:58:20

Why does filter work for an object, what does it do?

Understanding the creepy Vue Isotope plugin.
Here is what is used to filter elements:

filter: function (key) {
      this.$refs.cpt.filter(key)
    }

But it this.$refs.cpt's an object, not an array. I can't figure out what's going on, what is this code doing?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2018-03-27
@wufapexef

Apparently, because the plugin's source code has its own filter
function. Speaking of the possibility of using array methods on objects. Here is the code that works great:

const obj = {
  '0': 'some value 0',
  '1': 'some value 1',
  '2': false,
  length: 3,
};

const filteredArr = Array.prototype.filter.call(obj, el => el);
console.log(filteredArr); // ["0 value", "1 value"]

An array is just a special kind of object with a special set of keys and additional methods in the prototype.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question