My object is getting turned into an array
this.projects = {};
var outerThis = this;
for(var i=0; i<10; i++){
console.log(this.projects); // at this point projects is Object {}
if(!(i in Object.keys(this.projects))){
this.projects[i] = {title:"", trackOneUrl:"", trackTwoUrl:""};
console.log(this.projects); // now its [object object]
return;
}
}
when I do:
this.projects[i] = {title:"", trackOneUrl:"", trackTwoUrl:""}
projects goes from being an Object to an Array.
What I was trying to achieve is
{'1':{title:"", trackOneUrl:"", trackTwoUrl:""}}
instead I get
[{title:"", trackOneUrl:"", trackTwoUrl:""}]
3 Answers 3
Your this.projects will remain an object and it does exactly what you wanted it to be.
That [object Object] doesn't mean your projects becomes an array, that's a string representation of an object. Try calling String({}) and you will see the same effect.
Comments
this.projects is what is known as an object literal. {}
Create and array literal using []
From there you can assign an object to each index in the array using a simple for loop.
1 Comment
obj[] to access the properties on the object.What you have will work fine if you take out the return.
{ '0': { title: '', trackOneUrl: '', trackTwoUrl: '' } }which looks correct based on your desired outcome.[object Object]is not an array.