Javascript Typescript 数组映射 vs 过滤器 vs?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31174085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:20:39  来源:igfitidea点击:

Typescript array map vs filter vs?

javascripttypescript

提问by pitosalas

Here's a typescript method that wants to walk through an array of strings, and return another array of strings, where, strings that match the regexp (formatted something like "[la la la]" will become just "la la la" and strings that don't match are dropped. So that if my input array is:

这是一个想要遍历字符串数组并返回另一个字符串数组的打字稿方法,其中,与正则表达式匹配的字符串(格式化为“[la la la]”之类的内容将变为“la la la”和字符串不匹配被丢弃。所以如果我的输入数组是:

"[x]", "x", "[y]"

it becomes

它成为了

"x", "y"

Here's my code:

这是我的代码:

questions(): string[] {
    var regexp = /\[(.*)\]/;
    return this.rawRecords[0].map((value) => {
        console.log(value);
        var match = regexp.exec(value);
        if (match) {
            return match[1];
        }
    });
}

I end up with output like this:

我最终得到这样的输出:

"x", undefined, "y"

because of the "if (match)". What's the right typescript/javascript way of writing this code?

因为“如果(匹配)”。编写此代码的正确打字稿/javascript 方式是什么?

回答by basarat

Just filterthem out:

只是filter他们:

return this.rawRecords[0].map((value) => {
        console.log(value);
        var match = regexp.exec(value);
        if (match) {
            return match[1];
        }
    });
}).filter(x=>!!x);

回答by furkee

What map does is to apply your function to each and every element in your list. Your function does not return any value when regex is not matched. So, probably JS makes it return undefined for that cases. If you wanna drop the ones that do not match regexp, you should use filter right away.

map 的作用是将您的函数应用于列表中的每个元素。当正则表达式不匹配时,您的函数不会返回任何值。因此,对于这种情况,JS 可能会使其返回 undefined 。如果您想删除与正则表达式不匹配的那些,您应该立即使用过滤器。

Think about names of functions and you will have a better understanding right away(map maps a function to a list and filter filters a list).

想想函数的名称,你马上就会有更好的理解(map 将函数映射到列表并过滤器过滤列表)。

回答by Jeyanth

I tried out this problem and shared my result below. Hope this helps. Kindly apologize if I failed or misinterpreted,as I'm new to Typescript.

我尝试了这个问题并在下面分享了我的结果。希望这可以帮助。如果我失败或误解,请道歉,因为我是 Typescript 的新手。

var arr = ["[xy]","x","y"];
var ele =[];
var i;
var op;
var regex = /\[(.*)\]/;
var op1 = arr.filter((element)=>
{
    op =regex.exec(element);
    if(op)
    ele.push(op);
    else{
        ele.push(element);
        console.log(op);
    }
    });
    for(i =0;i<ele.length;i++)
    {
        console.log(ele[i]);
    }

回答by Jerska

I really believe that sometimes, we try to go way more functional than we should.
What is wrong with this:

我真的相信有时,我们会尝试比我们应该的更实用。
这有什么问题:

var regexp = /\[(.*)\]/;
var records = this.rawRecords[0];
var res = [];

for (var i = 0, len = records.length; i < len; ++i) {
    var match = regexp.exec(records[i]);
    if (match) {
        res.push(match[1]);
    }
});

return res;

It's not a one-liner, and even if I don't know anything about TypeScript (you also asked about JavaScript in your question), it should also be more efficient than any functional approach.

它不是单行的,即使我对 TypeScript 一无所知(您在问题中也询问了 JavaScript),它也应该比任何函数式方法更有效。

回答by marianc

Another option is to use reduce() methodlike this:

另一种选择是使用reduce() 方法,如下所示:

return this.rawRecords[0].reduce((acc, value) => {
    console.log(value);
    var match = regexp.exec(value);
    if (match) {
        acc.push(match[1]);
    }
    return acc;
}, []);

please check in JSFIDDLEthe result for the equivalent code in javascript.

请在JSFIDDLE中检查javascript中等效代码的结果。