The following function matches the strings in a file (queries
) with the strings of another two files (archives
). I included the important logs:
console.log('q:', queries)
console.log('a:', archives)
queries.forEach(query => {
const regex = new RegExp(query.trim(), 'g')
archives.forEach(archive => {
const matched = archive.match(regex)
console.log('m:', matched)
})
})
q: [ 'one two', 'three four\n' ]
a: [ 'one two three four three four\n', 'one two three four\n' ]
m: [ 'one two' ]
m: [ 'one two' ]
m: [ 'three four', 'three four' ]
m: [ 'three four' ]
How to modify the code so I merge matched
and end up with a result like this?
r1: [ 'one two', 'one two' ]
r2: [ 'three four', 'three four', 'three four' ]
(Maybe I can use .reduce
but I'm not very sure how.)
EDIT: I tried this:
const result = matched.reduce(function (a, b) {
return a.concat(b)
}, [])
But ended up with the same result.