using spread operator in json obejct

Hey everyone
3 Replies
NazCodeland
NazCodelandOP2y ago
vsc is throwing an error if I use the spread operator within a json object can't seem to find out the reason why we can't use spread operators in a json object, what's the reasoning behind why it doesn't work
const names = ['nameOne', 'nameTwo'];

const people = {
names: ...names}
const names = ['nameOne', 'nameTwo'];

const people = {
names: ...names}
when
const people = {
names: ['nameOne', "nameTwo"]}
const people = {
names: ['nameOne', "nameTwo"]}
would be fine, so I am wondering why is it that spread operators can't be used within JSON objects
vince
vince2y ago
They can I know it's a common pattern in React, I'm not an expert but try
const people = {
...names,
newKey: newValue,
}
const people = {
...names,
newKey: newValue,
}
NazCodeland
NazCodelandOP2y ago
the output of that is { '0': 'nameOne', '1': 'nameTwo' } which isn't the same as
{names: ['nameOne', "nameTwo"]}
{names: ['nameOne', "nameTwo"]}
didn't know we could do that though, that's good to know
const people = {
names: [...names],
};
const people = {
names: [...names],
};
that works got that idea from your help, awesome! thank you @vince
Want results from more Discord servers?
Add your server