最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Typescript array map vs filter vs? - Stack Overflow

programmeradmin4浏览0评论

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 bee just "la la la" and strings that don't match are dropped. So that if my input array is:

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

it bees

"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?

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 bee just "la la la" and strings that don't match are dropped. So that if my input array is:

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

it bees

"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?

Share Improve this question asked Jul 2, 2015 at 1:04 pitosalaspitosalas 10.9k15 gold badges77 silver badges131 bronze badges 1
  • 3 You could try .filter(Boolean) after that to get rid of undefineds. – elclanrs Commented Jul 2, 2015 at 1:10
Add a ment  | 

5 Answers 5

Reset to default 5

Just filter them out:

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

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.

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).

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.

Another option is to use reduce() method like this:

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 JSFIDDLE the result for the equivalent code in javascript.

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.

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]);
    }
发布评论

评论列表(0)

  1. 暂无评论