I am trying to push to an array on a new Map()
reduce from an array of objects, but for some reason it pushes only two items per array.
const data = [
{
keyword: 'engines',
url: 'google.com',
},
{
keyword: 'engines',
url: 'duckduckgo.com',
},
{
keyword: 'pc',
url: 'mac.com',
},
{
keyword: 'pc',
url: 'linux.com',
},
{
keyword: 'pc',
url: 'windows.com',
}
]
let urls = []
const mapper = data.reduce((acc, obj) => {
urls.push(obj.url)
if (!acc.has(obj.keyword)) {
acc.set(obj.keyword, urls )
} else {
urls = []
}
return acc
}, new Map())
console.log(mapper)
.as-console-wrapper { max-height: 100% !important; top: 0; }
Expected output
Map(2) {
'engines' => [ 'google.com', 'duckduckgo.com'],
'pc' => [ 'mac.com', 'linux.com', 'windows.com']
}
I dont even know why the snippet does not work on stackoverflow, it works fine on editor.
CodePudding user response:
I fixed it with this but is does not feel right
const data = [ { keyword: 'engines', url: 'google.com', }, { keyword: 'engines', url: 'duckduckgo.com', }, { keyword: 'pc', url: 'mac.com', }, { keyword: 'pc', url: 'linux.com', }, { keyword: 'pc', url: 'windows.com', } ]
let urls = []
const mapper = data.reduce((acc, obj) => {
if (!acc.has(obj.keyword)) {
urls = []
}
urls.push(obj.url)
acc.set(obj.keyword, urls )
return acc
}, new Map())
console.log(mapper)
CodePudding user response:
This gives you the correct output.
You need to use a new array for each keyword; otherwise you're sharing a single object and corrupting it as you go.
StackOverflow's REPL does not output Map
content in the same way as browsers when using console.log
, so I used a workaround to display the data.
const data = [
{
keyword: 'engines',
url: 'google.com',
},
{
keyword: 'engines',
url: 'duckduckgo.com',
},
{
keyword: 'pc',
url: 'mac.com',
},
{
keyword: 'pc',
url: 'linux.com',
},
{
keyword: 'pc',
url: 'windows.com',
}
]
const mapper = data.reduce((acc, obj) => {
if (!acc.has(obj.keyword)) {
acc.set(obj.keyword, [] )
}
let urls = acc.get(obj.keyword)
urls.push(obj.url);
return acc;
}, new Map())
/**
Expected output per OP:
Map(2) {
'engines' => [ 'google.com', 'duckduckgo.com'],
'pc' => [ 'mac.com', 'linux.com', 'windows.com']
}
*/
console.log([...mapper.entries()])
.as-console-wrapper { max-height: 100% !important; top: 0; }