我正在尝试将动态创建的Javascript对象添加到一个数组中。我可以遍历DOM并创建对象。但是在显示最终的对象数组时,计数是正确的,但所有对象都具有相同的值,即最终索引值。如何解决这个问题?
PS:DOM遍历和其他功能工作正常,唯一的问题是创建具有正确值的最终对象数组。
Javascript代码。
var match = {};
var matches = [];
$('.SIsort').each(function (i, v) {
console.log("type.."+typeof matches);
var date = $(this).find('td:eq(0)').find('meta')[0].content;
var team1 = $(this).find('td:eq(1)').find('div:eq(1)').text();
var team2 = $(this).find('td:eq(1)').find('div:eq(3)').text();
var loc = $(this).find('td:eq(2)').find('div:eq(0)').text();
match.date = date;
match.team1 = team1;
match.team2 = team2;
match.venue = loc;
console.log(match); // It displays Correctly
(matches = window.matches || []).push({});
matches = (window.matches || []).push(match);
// console.log(matches[i])
});
console.log(matches); // All object values belong only to final index