Why can't Javascript spread operator be used after object key?
up vote
2
down vote
favorite
I have the following code:
const array = [{
a: 'a',
b: 'b'
}];
console.log(...array);
const store = {
obj: ...array
}
console.log
will print the results just fine. However, when trying to set the key of store
I get a Parsing error: Unexpected token
.
Isn't ...array
a valid object to assign to the obj
key of store
?
javascript spread
add a comment |
up vote
2
down vote
favorite
I have the following code:
const array = [{
a: 'a',
b: 'b'
}];
console.log(...array);
const store = {
obj: ...array
}
console.log
will print the results just fine. However, when trying to set the key of store
I get a Parsing error: Unexpected token
.
Isn't ...array
a valid object to assign to the obj
key of store
?
javascript spread
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have the following code:
const array = [{
a: 'a',
b: 'b'
}];
console.log(...array);
const store = {
obj: ...array
}
console.log
will print the results just fine. However, when trying to set the key of store
I get a Parsing error: Unexpected token
.
Isn't ...array
a valid object to assign to the obj
key of store
?
javascript spread
I have the following code:
const array = [{
a: 'a',
b: 'b'
}];
console.log(...array);
const store = {
obj: ...array
}
console.log
will print the results just fine. However, when trying to set the key of store
I get a Parsing error: Unexpected token
.
Isn't ...array
a valid object to assign to the obj
key of store
?
javascript spread
javascript spread
asked Nov 10 at 18:20
Yos
332413
332413
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
5
down vote
accepted
...
spreads out array into individual items. Array can have more than 1 element and hence there will be more than 1 RHS and that will be invalid. Hence, you can use obj : {...array}
or obj : [...array]
const array = [{a: 'a',b: 'b'},{c: 'c', d: 'd'}];
console.log(...array);
const store = {
obj: {...array},
obj1: [...array]
};
console.log(store);
add a comment |
up vote
1
down vote
The spread syntax works inside of objects or iterable. In your case, you need to spread the elements within an array.
Spread Syntax
Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.
const array = [0, 1, 2]
const store = {
obj: [...array] // <-- the array is being spreded into an array.
}
console.log(store)
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
...
spreads out array into individual items. Array can have more than 1 element and hence there will be more than 1 RHS and that will be invalid. Hence, you can use obj : {...array}
or obj : [...array]
const array = [{a: 'a',b: 'b'},{c: 'c', d: 'd'}];
console.log(...array);
const store = {
obj: {...array},
obj1: [...array]
};
console.log(store);
add a comment |
up vote
5
down vote
accepted
...
spreads out array into individual items. Array can have more than 1 element and hence there will be more than 1 RHS and that will be invalid. Hence, you can use obj : {...array}
or obj : [...array]
const array = [{a: 'a',b: 'b'},{c: 'c', d: 'd'}];
console.log(...array);
const store = {
obj: {...array},
obj1: [...array]
};
console.log(store);
add a comment |
up vote
5
down vote
accepted
up vote
5
down vote
accepted
...
spreads out array into individual items. Array can have more than 1 element and hence there will be more than 1 RHS and that will be invalid. Hence, you can use obj : {...array}
or obj : [...array]
const array = [{a: 'a',b: 'b'},{c: 'c', d: 'd'}];
console.log(...array);
const store = {
obj: {...array},
obj1: [...array]
};
console.log(store);
...
spreads out array into individual items. Array can have more than 1 element and hence there will be more than 1 RHS and that will be invalid. Hence, you can use obj : {...array}
or obj : [...array]
const array = [{a: 'a',b: 'b'},{c: 'c', d: 'd'}];
console.log(...array);
const store = {
obj: {...array},
obj1: [...array]
};
console.log(store);
const array = [{a: 'a',b: 'b'},{c: 'c', d: 'd'}];
console.log(...array);
const store = {
obj: {...array},
obj1: [...array]
};
console.log(store);
const array = [{a: 'a',b: 'b'},{c: 'c', d: 'd'}];
console.log(...array);
const store = {
obj: {...array},
obj1: [...array]
};
console.log(store);
answered Nov 10 at 18:25
Nikhil Aggarwal
23.1k32647
23.1k32647
add a comment |
add a comment |
up vote
1
down vote
The spread syntax works inside of objects or iterable. In your case, you need to spread the elements within an array.
Spread Syntax
Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.
const array = [0, 1, 2]
const store = {
obj: [...array] // <-- the array is being spreded into an array.
}
console.log(store)
add a comment |
up vote
1
down vote
The spread syntax works inside of objects or iterable. In your case, you need to spread the elements within an array.
Spread Syntax
Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.
const array = [0, 1, 2]
const store = {
obj: [...array] // <-- the array is being spreded into an array.
}
console.log(store)
add a comment |
up vote
1
down vote
up vote
1
down vote
The spread syntax works inside of objects or iterable. In your case, you need to spread the elements within an array.
Spread Syntax
Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.
const array = [0, 1, 2]
const store = {
obj: [...array] // <-- the array is being spreded into an array.
}
console.log(store)
The spread syntax works inside of objects or iterable. In your case, you need to spread the elements within an array.
Spread Syntax
Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.
const array = [0, 1, 2]
const store = {
obj: [...array] // <-- the array is being spreded into an array.
}
console.log(store)
const array = [0, 1, 2]
const store = {
obj: [...array] // <-- the array is being spreded into an array.
}
console.log(store)
const array = [0, 1, 2]
const store = {
obj: [...array] // <-- the array is being spreded into an array.
}
console.log(store)
answered Nov 10 at 18:30
Ele
22.2k42044
22.2k42044
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%2f53242038%2fwhy-cant-javascript-spread-operator-be-used-after-object-key%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