Asked By: Anonymous
Spread operator(...
) for an object is not working as you can see in the below code. Is this expected behavior?
try it in jsbin
//var test ={start:'stating',end: 'ending'}; //its not working
var test=['start-value','end-value']; //its working
function display(start,end){
console.log(start,end);
}
display(...test)
The reason behind asking this question is, I saw the use of spread operator using an object in the below line of code but it’s not working for me.
Am I missing any configuration to make it work ?.
Solution
Answered By: Anonymous
From the docs:
Only for iterables
Note that the spread operator can be applied only to iterable objects:
var obj = {'key1': 'value1'};
var array = [...obj]; // TypeError: obj is not iterable
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator