i'm trying to pull out all of the data from my form and convert it to a queryString, to then post to an endpoint.
This is what I have so far, however I can#t figure out a quick and clean way to convert this to for example: key=value&key=value.
let data = Array.from(this.querySelectorAll('input:not([type="submit"]), select, textarea')).map(input => {
let value = '';
switch(input.tagName) {
case 'INPUT':
value = input.value;
break;
case 'SELECT':
value = input[input.selectedIndex].value;
break;
case 'TEXTAREA':
value = input.innerHTML;
break;
}
return {
key: input.name,
value: value
};
});
console.log(data);
// Object.keys(obj).map(k => `${encodeURIComponent(k)}=${encodeURIComponent(obj[k])}`).join('&');
The above code is creating an array of objects, with the key and values. It would be nice to be able use the one liner which is commented out above.
switch.this.valuewill work in all three cases..innerHTMLfor<textarea>is not correct; it's just.value. Also you should probably test for elements that aredisabled, for completeness.be nice to be able use the one liner->data.map(({key, value}) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`).join('&');