I have an array that looks like this:
let movies = [
'terminator.1',
'terminator.2',
'terminator.3',
'harry-potter.1',
'harry-potter.3',
'harry-potter.2',
'star-wars.1'
]
and I would like to have an object like this:
{
"terminator": [1,2,3],
"harry-potter": [1,2,3],
"star-wars": [1]
}
so far I'm able to have an object like this
{
{ terminator: [ '1' ] },
{ terminator: [ '2' ] },
{ terminator: [ '3' ] },
{ 'harry-potter': [ '1' ] },
{ 'harry-potter': [ '3' ] },
{ 'harry-potter': [ '2' ] },
{ 'star-wars': [ '1' ] }
}
I would like to know if there is a way to check during an Array.map when I'm generating my object if there is already a certain key and if there is to push the value to the corresponding array instead of creating a new key-value pair.
This is the code that I currently use for my solution. Thanks in advance.
let movies = [
'terminator.1',
'terminator.2',
'terminator.3',
'harry-potter.1',
'harry-potter.3',
'harry-potter.2',
'star-wars.1'
]
let t = movies.map(m => {
let [name, number] = [m.split('.')[0],m.split('.')[1]]
return {[name]: [number]}
})
console.log(t)