简体   繁体   中英

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!

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";
        });
    });

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 .

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM