Having trouble finding the right keyword to search for my issue, as I am confused about JavaScript or TypeScript spread operator behavior. I encountered this problem while working on a drag and drop function today, but let me provide a simple example. Imagine I have an array like this:
test = [
{
name: 'A',
id: 1
},
{
name: 'B',
id: 2
},
{
name: 'C',
id: 3
}
]
Let's say I want to create a shallow copy of this array. Initially, I did it in the following way without realizing the mistake:
My first code attempt looked like this:
const tempArray = { ...test }
console.log('check data : ', tempArray) // will result in an object instead of an array
Upon rechecking my code, I realized my error and changed it to:
const tempArray = [...test]
console.log('check data : ', tempArray) // now correctly results in an array of objects
If you can explain why both versions seem to work fine and display the same correct result, please help me identify the specific term describing this behavior so I can research further.
EDIT 1:
https://i.sstatic.net/4VawW.png
After taking another look, I see that the two methods actually produce different outcomes. The object version appears similar to an array, but it is creating an object with numbers as keys.