I have an array containing some IDs:
let ids = [ '184', '085', '241', '086', '087', '165' ];
and an empty object - let data = {};
I am looping through the array and setting each of the elements as a property of the object:
ids.forEach(id => {
data[id] = '';
});
I expect the data object to be:
{ '184': '', '085': '', '241': '', '086': '', '087': '', '165': '' }
But when I console.log(data)
I find that the order of the properties is messed up and data
is:
{ '165': '', '184': '', '241': '', '085': '', '086': '', '087': '' }
I have tried looping through the array with this method also:
for (let id of ids) {
data[id] = '';
}
But the same messed-up order results. I need to know how can I set the properties in correct order and, more importantly, what is causing this issue.