每个循环只显示最后一个实例
Each loop only showing last instance
您好,我的每个循环都有问题。当我 运行 外部 each 循环时,记录的数据多次显示最后一个实例,而不是显示每个实例。
问题似乎出在 .push 上,因为 the_data 的日志工作正常
//Set variables
var links = [];
var the_data = [];
//Loop through each instance of .section
$.each($('.section'), function(index, item) {
//find all instances of .item within .section
var data = $(this).find('.Item');
//loop through each instance of .item and find all data attributes and add them to an array
$(data).each(function() {
var name = $(this).attr('data-name');
the_data[name] = $(this).val();
});
//log the_data to check
console.log(the_data);
//push the_data array into links
links.push(the_data);
});
//log links to check
console.log(links);
控制台输出如下:
[articleTitle: "title", articleOutlet: "outlet", articleLink: "link", articleExcerpt: "Excerpt"]
[articleTitle: "title 2", articleOutlet: "outlet 2", articleLink: "link 2", articleExcerpt: "Excerpt 2"]
[Array[0], Array[0]]0:
Array[0]articleExcerpt: "Excerpt 2"articleLink: "link 2"articleOutlet: "outlet 2"articleTitle: "title 2"length: 0__proto__: Array[0]1:
Array[0]articleExcerpt: "Excerpt 2"articleLink: "link 2"articleOutlet: "outlet 2"articleTitle: "title 2"length: 0__proto__: Array[0]length: 2__proto__: Array[0]
您似乎正在用第二个条目覆盖第一个条目。
the_data
是单个数组,在您的示例中初始化一次。您的代码每次都将同一个数组推送到 links
,因此当然它不止一次显示相同的数据。
您需要将 the_data
重置为外部 each
循环内的新数组
$.each($('.section'), function(index, item) {
the_data = [];
或者只是在外循环中声明它:
$.each($('.section'), function(index, item) {
var the_data = [];
由于您似乎将 the_date
用作 name/value 字典,而不是数组,您不妨将其声明为对象:
$.each($('.section'), function(index, item) {
var the_data = {};
每次执行 links.push(the_data)
时,您都会将同一个数组推入 links
数组。它不会复制它。您应该在外部 $.each()
循环内初始化 the_data
。此外,看起来 the_data
应该是一个对象,而不是数组,因为您要将 name: value
对存储到其中。数组用于数字索引数据。
//Loop through each instance of .section
$.each($('.section'), function(index, item) {
var the_data = {};
...
}
您好,我的每个循环都有问题。当我 运行 外部 each 循环时,记录的数据多次显示最后一个实例,而不是显示每个实例。
问题似乎出在 .push 上,因为 the_data 的日志工作正常
//Set variables
var links = [];
var the_data = [];
//Loop through each instance of .section
$.each($('.section'), function(index, item) {
//find all instances of .item within .section
var data = $(this).find('.Item');
//loop through each instance of .item and find all data attributes and add them to an array
$(data).each(function() {
var name = $(this).attr('data-name');
the_data[name] = $(this).val();
});
//log the_data to check
console.log(the_data);
//push the_data array into links
links.push(the_data);
});
//log links to check
console.log(links);
控制台输出如下:
[articleTitle: "title", articleOutlet: "outlet", articleLink: "link", articleExcerpt: "Excerpt"]
[articleTitle: "title 2", articleOutlet: "outlet 2", articleLink: "link 2", articleExcerpt: "Excerpt 2"]
[Array[0], Array[0]]0:
Array[0]articleExcerpt: "Excerpt 2"articleLink: "link 2"articleOutlet: "outlet 2"articleTitle: "title 2"length: 0__proto__: Array[0]1:
Array[0]articleExcerpt: "Excerpt 2"articleLink: "link 2"articleOutlet: "outlet 2"articleTitle: "title 2"length: 0__proto__: Array[0]length: 2__proto__: Array[0]
您似乎正在用第二个条目覆盖第一个条目。
the_data
是单个数组,在您的示例中初始化一次。您的代码每次都将同一个数组推送到 links
,因此当然它不止一次显示相同的数据。
您需要将 the_data
重置为外部 each
循环内的新数组
$.each($('.section'), function(index, item) {
the_data = [];
或者只是在外循环中声明它:
$.each($('.section'), function(index, item) {
var the_data = [];
由于您似乎将 the_date
用作 name/value 字典,而不是数组,您不妨将其声明为对象:
$.each($('.section'), function(index, item) {
var the_data = {};
每次执行 links.push(the_data)
时,您都会将同一个数组推入 links
数组。它不会复制它。您应该在外部 $.each()
循环内初始化 the_data
。此外,看起来 the_data
应该是一个对象,而不是数组,因为您要将 name: value
对存储到其中。数组用于数字索引数据。
//Loop through each instance of .section
$.each($('.section'), function(index, item) {
var the_data = {};
...
}