jquery - Javascript object property 'null' when iterating through an array of objects -
i have array of objects. somehow, 1 of properties goes missing within each loop. have tried many different types of loops , nothing resolves issue.
somefunction(somearrayofobjects) { console.log(somearrayofobjects); // logs objects expect them $.each(somearrayofobjects, function(index, someobject) { console.log(someobject); // 1 of properties of someobject 'null' }); }
update: how array constructed:
// constructor var people = []; var person = function (name, age, photo) { this.name = name; this.age = age; this.photo = null; };
then use ajax json , create objects
success: function(data) { people.push(new person(data['name'], data['age']) }
then iterate through each of people , make ajax request each of photos. callback looks this:
function(person, photo) { person.photo = photo; });
i new javascript , async programming wrapping mind around callbacks tough. thought had figured out when console.log(somearrayofobjects)
worked expected. can't figure out why photo property reverts when objects logged individually. tried rewriting in coffeescript because syntax bit easier me, unfortunately issue remains.
thank you.
screenshot of console:
new update , final answer @ last
below sample code, here after ajax calls finished buildquiz() function called meaning data ready further use.
$.when( $.ajax( "/page1.php" ), $.ajax( "/page2.php" ) ) .then( buildquiz, myfailure );
old update
as of now(might 3 times but) try below code , let me know log..
function getcastmembers(movietitle) { getnames(movietitle, function(castmembers) { getphotos(castmembers, function(castmember, photo) { castmember.photo = photo; buildquiz(castmembers); }); }); }
old update
so appears there has going on between first , sceond log statements.
var people = []; var person = function (name, age, photo) { this.name = name; this.age = age; this.photo = null; }; var somearrayofobjects = [ {name: "joe", age: "32", photo: "joe.jpg"}, {name: "alice", age: "5", photo: "alice.jpg"},{name: "john", age:"22", photo:"john.jpg"}]; for(i in somearrayofobjects){ people.push(new person(somearrayofobjects[i]['name'], somearrayofobjects[i]['age'])) } people.foreach(function(person,index){ person.photo = somearrayofobjects[index].photo; }) function somefunction(people){ console.log(people); // logs objects expect them people.foreach(function(person) { console.log(person); }) } somefunction(people); output vm359:17 [person, person, person] 0: personage: "32"name: "joe"photo: "joe.jpg" __proto__: person1: person2: personlength: 3__proto__: array[0] vm359:19 person {name: "joe", age: "32", photo: "joe.jpg"} vm359:19 person {name: "alice", age: "5", photo: "alice.jpg"} vm359:19 person {name: "john", age: "22", photo: "john.jpg"}
old update
in success callback of photo
data call function iterate.
success: function(data) { for(){ person.photo = data.photo; // maybe // have within loop // or } // after persons filled // photo call iterator somefunction(somearrayofobjects); }
old answer
below code works fine. try out.
var somearrayofobjects = [ {name: "joe", age: "32", photo: "joe.jpg"}, {name: "alice", age: "5", photo: "alice.jpg"}, {name: "john", age: "22", photo: "john.jpg"} ]; function somefunction(somearrayofobjects){ console.log(somearrayofobjects); // logs objects expect them somearrayofobjects.foreach(function(someobj) { console.log(someobj.name +" "+someobj.age +" "+ someobj.photo); }) } somefunction(somearrayofobjects); /* outout */ /* joe 32 joe.jpg alice 5 alice.jpg john 22 john.jpg */
Comments
Post a Comment