Javascript Array.filter() and Array.some()

Viewed 1938

I want to filter an array of files by their extensions. I have written following code:

    const exts = ['.log, ts']

    const files = [ 
        'a.ts', 'b.xml', 'c.log'
    ]

    const res = files.filter(f => {
        return exts.some(e => {
            return f.endsWith(e)
        })
    })

   console.log(res)

In my opinion, it should output['a.ts', 'c.log'].

But I get an empty array.

I am looking at this since hours. I don't get it. Please help me.. What's wrong ?

1 Answers

You need more than one string in the array for exts

const exts = ['.log', '.ts']

const
    exts = ['.log', '.ts'],
    files = ['a.ts', 'b.xml', 'c.log'],
    res = files.filter(f => exts.some(e => f.endsWith(e)));

console.log(res);

Related