Javascript object 属性 'null' 遍历对象数组时

Javascript object property 'null' when iterating through an array of objects

我有一组对象。不知何故,其中一个属性在我的每个循环中丢失了。我尝试了很多不同类型的循环,但没有解决这个问题。

someFunction(someArrayOfObjects) {
    console.log(someArrayOfObjects); // logs objects as I expect them to be

    $.each(someArrayOfObjects, function(index, someObject) {
        console.log(someObject); // one of the properties of someObject is 'null'
    });
}

更新:数组的构造方式:

// constructor
var people = [];
var Person = function (name, age, photo) {
    this.name = name;
    this.age = age;
    this.photo = null;
};

然后使用ajax得到一些JSON并创建对象

    success: function(data) {
        people.push(new Person(data['name'], data['age'])
}

然后我遍历每个人并发出另一个 AJAX 请求以获取他们的每张照片。回调看起来像这样:

function(person, photo) {
    person.photo = photo;
});

我是 javascript 和异步编程的新手,所以我很难全神贯注于回调。当 console.log(someArrayOfObjects) 按我的预期工作时,我以为我已经弄明白了。我只是想不通为什么当单独记录对象时照片 属性 会还原。 我也尝试用 CoffeeScript 重写所有内容,因为语法对我来说更容易一些,但不幸的是问题仍然存在。

谢谢。

控制台截图:

最新更新和最后的答案

下面是示例代码,在所有 ajax 调用完成后,只有 buildQuiz() 函数会被调用,这意味着所有数据都已准备好供进一步使用.

$.when( $.ajax( "/page1.php" ), $.ajax( "/page2.php" ) )
  .then( buildQuiz, myFailure );

旧更新

截至目前(可能会得到 3 次但是)只需尝试下面的代码并让我知道日志..

function getCastMembers(movieTitle) {
  getNames(movieTitle, function(castMembers) {
    getPhotos(castMembers, function(castMember, photo) {
      castMember.photo = photo;
      buildQuiz(castMembers);
    });
  });
}

旧更新

所以看来第一个和第二个日志语句之间必须发生某些事情。

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 as I expect them to be
       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"}

旧更新

photo 数据的成功回调中调用函数进行迭代。

success: function(data) {
     for(){
         person.photo = data.photo;  // maybe something like this
                                     // you have within for loop
                                     // or something
    }
    // so after all persons filled
    // up with photo now call iterator
    somefunction(someArrayOfObjects);
}

旧答案

下面的代码工作正常。试试吧。

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 as I expect them to be
    
        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
*/