Javascript对象属性' null'在迭代对象数组时

时间:2015-06-14 11:59:53

标签: javascript jquery ajax iteration

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

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重写所有内容,因为语法对我来说更容易,但不幸的是问题仍然存在。

谢谢。

控制台的屏幕截图: screenshot of console

1 个答案:

答案 0 :(得分:1)

最新消息和最终答案

下面是示例代码,在完成所有ajax调用之后,只会调用 buildQuiz()函数,这意味着所有数据都可以继续使用。

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

OLD UPDATE

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

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

OLD UPDATE

所以看来首先和sceond日志语句之间必然会发生一些事情。

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"}

OLD UPDATE

成功回调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);
}

OLD ANSWER

下面的代码工作正常。尝试一下。



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
*/