NickVH
NickVH

Reputation: 143

Rewriting an Javascript Arrow-function

Can anyone explain and help me how to re-code this Javascript Arrow-function?

var Data = JSArray.filter(v => v.tags.some(k => k.name === "test"));

I'd just like to translate it to Javascript default functions instead of an Arrow-function.

Thanks!

Upvotes: 0

Views: 1668

Answers (3)

str
str

Reputation: 44979

You can easily do this using the Babel.js Tryout

"use strict";

var Data = JSArray.filter(function (v) {
  return v.tags.some(function (k) {
    return k.name === "test";
  });
});

For an explanation see MDN: Arrow functions.

Upvotes: 2

fafl
fafl

Reputation: 7385

You have a quite pretty one-liner here, without arrow functions it gets more verbose. Try something like this:

var Data = [];
for (var i = 0; i < JSArray.length; i++) {
    var e = JSArray[i];
    if (e.tags.some(function(k) {return k.name === "test"})) {
        Data.push(e);
    }
}

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386654

You could change an arrow function () => expression, from

a => a

to

function (a) {
    return a;
}

Together, you get

var Data = JSArray.filter(function (v) {
        return v.tags.some(function (k) {
            return k.name === "test";
        });
    });

Upvotes: 3

Related Questions