$dataItems.find(...) doesn't work ?

Hi everyone,

I tried to call the method find() with a suitable function as parameter...

However, I get a crash telling me "Undefined is not a function" while pointing me on the method "find()".

Isn't $dataItems an array? Did I miss something?

Thanks for your help,
Kohlskui
MV doesn't use ES6 so find doesn't work. You have to use filter and return result 0: $dataItems.filter(function(item) { return [CONDITION HERE]; })[0];
Thanks for your help, Trihan.

It looks like a heavy function to me, I hope it doesn't slowdown anything... But atleast it works.
I think internally find does more or less the same thing so it shouldn't cause any slowdown.

There is a snippet on the Mozilla Developer portal's Javascript reference that lets you polyfill the find function where it isn't yet available:

Code:
// https://tc39.github.io/ecma262/#sec-array.prototype.find
if (!Array.prototype.find) {
  Object.defineProperty(Array.prototype, 'find', {
    value: function(predicate) {
     // 1. Let O be ? ToObject(this value).
      if (this == null) {
        throw new TypeError('"this" is null or not defined');
      }

      var o = Object(this);

      // 2. Let len be ? ToLength(? Get(O, "length")).
      var len = o.length >>> 0;

      // 3. If IsCallable(predicate) is false, throw a TypeError exception.
      if (typeof predicate !== 'function') {
        throw new TypeError('predicate must be a function');
      }

      // 4. If thisArg was supplied, let T be thisArg; else let T be undefined.
      var thisArg = arguments[1];

      // 5. Let k be 0.
      var k = 0;

      // 6. Repeat, while k < len
      while (k < len) {
        // a. Let Pk be ! ToString(k).
        // b. Let kValue be ? Get(O, Pk).
        // c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
        // d. If testResult is true, return kValue.
        var kValue = o[k];
        if (predicate.call(thisArg, kValue, k, o)) {
          return kValue;
        }
        // e. Increase k by 1.
        k++;
      }

      // 7. Return undefined.
      return undefined;
    }
  });
}
The only time you need to worry about your functions performance like that (micro optimizations) is if you are calling it every frame. I use filter, forEach, map, reduce very often and it never affects performance.

As for the use of ES6, I write all my plugins in ES6 then transpile with babel. So if you want to use find(), just setup babel and transpile your code.

Original thread: https://forums.rpgmakerweb.com/threads/dataitems-find-doesnt-work.77597/