Let's say I have a form, and I want to check what inputs has been declared in the form, so I do something like this:
let inputs = {};
['first_name', 'last_name', ...].forEach(name => {
let input = document.querySelector(`input[name="${name}"]`);
if (input !== undefined)
inputs = Object.assign(inputs, {[input.getAttribute('name')]: input.value});
}
In this case, so far so good.
But what if have some inputs called ['name.first', 'name.last']? then, it will store the data to an object like this:
{name.first: '...', name.last: '...'}.
and not like this: (which is my desired result)
{
name: {
first: '...',
last: '...'
}
}
How can I tell JavaScript to parse it as an object path instead of a key string?
forEachor evenreduceas you are not actually performing amap. I'm also curious as to why you use a fixed array of names to filter results? Perhaps you could explain a little more about what you are doing?mapinstead offorEachwasn't smart at all. It was a mistake.