string to indexed property
up vote
1
down vote
favorite
I have a, next to a couple of fixed options, a variable number of yes/no radio inputs named other[index]
. Using $(form).serializeArray()
I get an array of name/value objects. Using reduce I'm able to reduce em down to an actual object.
const seializedForm = $(event.currentTarget.form).serializeArray();
const gdpr = seializedForm.reduce((aggragation, option) => {
return {
...aggragation,
[option.name]: option.value === 'true'
}}, {});
The problem here is that the result isn't exactly what I need:
{
"canNotify":true,
"canContact":true,
"canProcess":true,
"other[0]":false,
"other[1]":true,
"other[2]":false
}
I'd like it to be:
{
"canNotify":true,
"canContact":true,
"canProcess":true,
"other": [
false,
true,
false
]
}
Any suggestions?
javascript arrays
add a comment |
up vote
1
down vote
favorite
I have a, next to a couple of fixed options, a variable number of yes/no radio inputs named other[index]
. Using $(form).serializeArray()
I get an array of name/value objects. Using reduce I'm able to reduce em down to an actual object.
const seializedForm = $(event.currentTarget.form).serializeArray();
const gdpr = seializedForm.reduce((aggragation, option) => {
return {
...aggragation,
[option.name]: option.value === 'true'
}}, {});
The problem here is that the result isn't exactly what I need:
{
"canNotify":true,
"canContact":true,
"canProcess":true,
"other[0]":false,
"other[1]":true,
"other[2]":false
}
I'd like it to be:
{
"canNotify":true,
"canContact":true,
"canProcess":true,
"other": [
false,
true,
false
]
}
Any suggestions?
javascript arrays
Can you add the form?
– ksav
Nov 10 at 21:18
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a, next to a couple of fixed options, a variable number of yes/no radio inputs named other[index]
. Using $(form).serializeArray()
I get an array of name/value objects. Using reduce I'm able to reduce em down to an actual object.
const seializedForm = $(event.currentTarget.form).serializeArray();
const gdpr = seializedForm.reduce((aggragation, option) => {
return {
...aggragation,
[option.name]: option.value === 'true'
}}, {});
The problem here is that the result isn't exactly what I need:
{
"canNotify":true,
"canContact":true,
"canProcess":true,
"other[0]":false,
"other[1]":true,
"other[2]":false
}
I'd like it to be:
{
"canNotify":true,
"canContact":true,
"canProcess":true,
"other": [
false,
true,
false
]
}
Any suggestions?
javascript arrays
I have a, next to a couple of fixed options, a variable number of yes/no radio inputs named other[index]
. Using $(form).serializeArray()
I get an array of name/value objects. Using reduce I'm able to reduce em down to an actual object.
const seializedForm = $(event.currentTarget.form).serializeArray();
const gdpr = seializedForm.reduce((aggragation, option) => {
return {
...aggragation,
[option.name]: option.value === 'true'
}}, {});
The problem here is that the result isn't exactly what I need:
{
"canNotify":true,
"canContact":true,
"canProcess":true,
"other[0]":false,
"other[1]":true,
"other[2]":false
}
I'd like it to be:
{
"canNotify":true,
"canContact":true,
"canProcess":true,
"other": [
false,
true,
false
]
}
Any suggestions?
javascript arrays
javascript arrays
asked Nov 10 at 21:13
Sylvain Girard
9918
9918
Can you add the form?
– ksav
Nov 10 at 21:18
add a comment |
Can you add the form?
– ksav
Nov 10 at 21:18
Can you add the form?
– ksav
Nov 10 at 21:18
Can you add the form?
– ksav
Nov 10 at 21:18
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
For each name - remove the brackets, and if the key already exists in the array, combine the values to an array using array spread:
const serializedForm = [{"name":"canNotify","value":"true"},{"name":"canContact","value":"true"},{"name":"canProcess","value":"false"},{"name":"other[0]","value":"false"},{"name":"other[1]","value":"true"},{"name":"other[2]","value":"false"}];
const gdpr = serializedForm.reduce((aggragation, { name, value }) => {
const isArray = name.includes('[');
const key = name.replace(/[.+]/g, '');
const val = value === 'true';
return {
...aggragation,
[key]: isArray ? [...aggragation[key] || , val] : val
};
}, {});
console.log(gdpr);
@customcommander - Yep. It should always be an array, even if it has one item. Fixed.
– Ori Drori
Nov 11 at 5:11
I was hoping for a way without processing strings but this does the trick and is the most complete answer. Thanks!
– Sylvain Girard
Nov 11 at 6:18
add a comment |
up vote
0
down vote
Without knowing what the full object structure looks like, why not just check what the name contains before returning, if the name contains the array syntax. or the string
other
, then we can assume that it is part of the other form collection structure?
const seializedForm = $(event.currentTarget.form).serializeArray();
const gdpr = seializedForm.reduce((aggragation, option) => {
if (isInArrayOfOptions(option)) {
return {
...aggragation,
/* Return a new array combining the current with the next option.value*/
'other': [...aggragation.other, ...[option.value === 'true']]
}
}
return {
...aggragation,
[option.name]: option.value === 'true'
}
}, {});
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
For each name - remove the brackets, and if the key already exists in the array, combine the values to an array using array spread:
const serializedForm = [{"name":"canNotify","value":"true"},{"name":"canContact","value":"true"},{"name":"canProcess","value":"false"},{"name":"other[0]","value":"false"},{"name":"other[1]","value":"true"},{"name":"other[2]","value":"false"}];
const gdpr = serializedForm.reduce((aggragation, { name, value }) => {
const isArray = name.includes('[');
const key = name.replace(/[.+]/g, '');
const val = value === 'true';
return {
...aggragation,
[key]: isArray ? [...aggragation[key] || , val] : val
};
}, {});
console.log(gdpr);
@customcommander - Yep. It should always be an array, even if it has one item. Fixed.
– Ori Drori
Nov 11 at 5:11
I was hoping for a way without processing strings but this does the trick and is the most complete answer. Thanks!
– Sylvain Girard
Nov 11 at 6:18
add a comment |
up vote
2
down vote
accepted
For each name - remove the brackets, and if the key already exists in the array, combine the values to an array using array spread:
const serializedForm = [{"name":"canNotify","value":"true"},{"name":"canContact","value":"true"},{"name":"canProcess","value":"false"},{"name":"other[0]","value":"false"},{"name":"other[1]","value":"true"},{"name":"other[2]","value":"false"}];
const gdpr = serializedForm.reduce((aggragation, { name, value }) => {
const isArray = name.includes('[');
const key = name.replace(/[.+]/g, '');
const val = value === 'true';
return {
...aggragation,
[key]: isArray ? [...aggragation[key] || , val] : val
};
}, {});
console.log(gdpr);
@customcommander - Yep. It should always be an array, even if it has one item. Fixed.
– Ori Drori
Nov 11 at 5:11
I was hoping for a way without processing strings but this does the trick and is the most complete answer. Thanks!
– Sylvain Girard
Nov 11 at 6:18
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
For each name - remove the brackets, and if the key already exists in the array, combine the values to an array using array spread:
const serializedForm = [{"name":"canNotify","value":"true"},{"name":"canContact","value":"true"},{"name":"canProcess","value":"false"},{"name":"other[0]","value":"false"},{"name":"other[1]","value":"true"},{"name":"other[2]","value":"false"}];
const gdpr = serializedForm.reduce((aggragation, { name, value }) => {
const isArray = name.includes('[');
const key = name.replace(/[.+]/g, '');
const val = value === 'true';
return {
...aggragation,
[key]: isArray ? [...aggragation[key] || , val] : val
};
}, {});
console.log(gdpr);
For each name - remove the brackets, and if the key already exists in the array, combine the values to an array using array spread:
const serializedForm = [{"name":"canNotify","value":"true"},{"name":"canContact","value":"true"},{"name":"canProcess","value":"false"},{"name":"other[0]","value":"false"},{"name":"other[1]","value":"true"},{"name":"other[2]","value":"false"}];
const gdpr = serializedForm.reduce((aggragation, { name, value }) => {
const isArray = name.includes('[');
const key = name.replace(/[.+]/g, '');
const val = value === 'true';
return {
...aggragation,
[key]: isArray ? [...aggragation[key] || , val] : val
};
}, {});
console.log(gdpr);
const serializedForm = [{"name":"canNotify","value":"true"},{"name":"canContact","value":"true"},{"name":"canProcess","value":"false"},{"name":"other[0]","value":"false"},{"name":"other[1]","value":"true"},{"name":"other[2]","value":"false"}];
const gdpr = serializedForm.reduce((aggragation, { name, value }) => {
const isArray = name.includes('[');
const key = name.replace(/[.+]/g, '');
const val = value === 'true';
return {
...aggragation,
[key]: isArray ? [...aggragation[key] || , val] : val
};
}, {});
console.log(gdpr);
const serializedForm = [{"name":"canNotify","value":"true"},{"name":"canContact","value":"true"},{"name":"canProcess","value":"false"},{"name":"other[0]","value":"false"},{"name":"other[1]","value":"true"},{"name":"other[2]","value":"false"}];
const gdpr = serializedForm.reduce((aggragation, { name, value }) => {
const isArray = name.includes('[');
const key = name.replace(/[.+]/g, '');
const val = value === 'true';
return {
...aggragation,
[key]: isArray ? [...aggragation[key] || , val] : val
};
}, {});
console.log(gdpr);
edited Nov 11 at 5:16
answered Nov 10 at 21:33
Ori Drori
70.9k127388
70.9k127388
@customcommander - Yep. It should always be an array, even if it has one item. Fixed.
– Ori Drori
Nov 11 at 5:11
I was hoping for a way without processing strings but this does the trick and is the most complete answer. Thanks!
– Sylvain Girard
Nov 11 at 6:18
add a comment |
@customcommander - Yep. It should always be an array, even if it has one item. Fixed.
– Ori Drori
Nov 11 at 5:11
I was hoping for a way without processing strings but this does the trick and is the most complete answer. Thanks!
– Sylvain Girard
Nov 11 at 6:18
@customcommander - Yep. It should always be an array, even if it has one item. Fixed.
– Ori Drori
Nov 11 at 5:11
@customcommander - Yep. It should always be an array, even if it has one item. Fixed.
– Ori Drori
Nov 11 at 5:11
I was hoping for a way without processing strings but this does the trick and is the most complete answer. Thanks!
– Sylvain Girard
Nov 11 at 6:18
I was hoping for a way without processing strings but this does the trick and is the most complete answer. Thanks!
– Sylvain Girard
Nov 11 at 6:18
add a comment |
up vote
0
down vote
Without knowing what the full object structure looks like, why not just check what the name contains before returning, if the name contains the array syntax. or the string
other
, then we can assume that it is part of the other form collection structure?
const seializedForm = $(event.currentTarget.form).serializeArray();
const gdpr = seializedForm.reduce((aggragation, option) => {
if (isInArrayOfOptions(option)) {
return {
...aggragation,
/* Return a new array combining the current with the next option.value*/
'other': [...aggragation.other, ...[option.value === 'true']]
}
}
return {
...aggragation,
[option.name]: option.value === 'true'
}
}, {});
add a comment |
up vote
0
down vote
Without knowing what the full object structure looks like, why not just check what the name contains before returning, if the name contains the array syntax. or the string
other
, then we can assume that it is part of the other form collection structure?
const seializedForm = $(event.currentTarget.form).serializeArray();
const gdpr = seializedForm.reduce((aggragation, option) => {
if (isInArrayOfOptions(option)) {
return {
...aggragation,
/* Return a new array combining the current with the next option.value*/
'other': [...aggragation.other, ...[option.value === 'true']]
}
}
return {
...aggragation,
[option.name]: option.value === 'true'
}
}, {});
add a comment |
up vote
0
down vote
up vote
0
down vote
Without knowing what the full object structure looks like, why not just check what the name contains before returning, if the name contains the array syntax. or the string
other
, then we can assume that it is part of the other form collection structure?
const seializedForm = $(event.currentTarget.form).serializeArray();
const gdpr = seializedForm.reduce((aggragation, option) => {
if (isInArrayOfOptions(option)) {
return {
...aggragation,
/* Return a new array combining the current with the next option.value*/
'other': [...aggragation.other, ...[option.value === 'true']]
}
}
return {
...aggragation,
[option.name]: option.value === 'true'
}
}, {});
Without knowing what the full object structure looks like, why not just check what the name contains before returning, if the name contains the array syntax. or the string
other
, then we can assume that it is part of the other form collection structure?
const seializedForm = $(event.currentTarget.form).serializeArray();
const gdpr = seializedForm.reduce((aggragation, option) => {
if (isInArrayOfOptions(option)) {
return {
...aggragation,
/* Return a new array combining the current with the next option.value*/
'other': [...aggragation.other, ...[option.value === 'true']]
}
}
return {
...aggragation,
[option.name]: option.value === 'true'
}
}, {});
answered Nov 10 at 21:33
Kyle
28914
28914
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243464%2fstring-to-indexed-property%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you add the form?
– ksav
Nov 10 at 21:18