2015-06-14 168 views
2

我有一个对象数组。不知何故,其中一个属性在我的每个循环内都会丢失。我尝试了许多不同类型的循环,但没有任何办法可以解决这个问题。当通过对象数组迭代时,Javascript对象属性'null'

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' 
    }); 
} 

UPDATE:如何数组构造:

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

+0

你能提供阵列的简单的例子? – user3272018

+0

'var someArrayOfObjects = {name:“joe”,age:“32”,photo:“joe.jpg”}, {name:“alice”,年龄:“5”,照片:“alice.jpg” }, {name:“john”,age:“22”,photo:“john.jpg”} ]'@ user3272018 – lizvdk

+0

给出的代码似乎是正确的。 – vinayakj

回答

1

新的更新,并在最后

下面的最终答案是示例代码,所有的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); 
    }); 
    }); 
} 

旧的更新

所以它似乎必须有一些之间的第一和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"} 

旧的更新

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

+0

评论不适用于扩展讨论;这个对话已经[转移到聊天](http://chat.stackoverflow.com/rooms/80941/discussion-on-answer-by-vinayakj-javascript-object-property-null-when-iteratin)。 –