繁体   English   中英

遍历对象数组时的 Javascript 对象属性“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)按预期工作时,我以为我已经弄清楚了。 我只是无法弄清楚为什么在单独记录对象时 photo 属性会恢复。 我还尝试在 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 */

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM