[英]Why are the object values getting pushed into the array 3 times?
I have a simple object array into which I am pushing an object with 2 fields: bucketName and Date. 我有一个简单的对象数组,其中将2个字段推入一个对象:bucketName和Date。 The problem is that the values are getting pushed thrice into the array.
问题是这些值被三次推送到数组中。 Please help me.
请帮我。
JS: JS:
sortBucket: function(bucketList) {
var counter, j = 0;
var str = "aws-billing-csv";
console.log("Bucket List :: ", bucketList);
bucketList.forEach(function(bucket, index) {
(function(bucketId) {
var bucketObj = {};
// console.log("Bucket Id :: ",bucketId);
s3Client.listObjects(params = {Bucket: bucketId }, function(err, data) {
var csvBucketArr = [];
if (err) {
document.getElementById('status').innerHTML = 'Could not load objects from ' + bucketID;
}
else{
//console.log("Bucket Data "+index+" :: ",data);
data.Contents.forEach(function(content,contentIndex){
var fileKey = content.Key;
if(fileKey.search(str) != -1) {
// console.log("fileKey["+bucketId+"] "+contentIndex+" :: ",fileKey + " Date :: " ,content.LastModified);
bucketObj[fileKey] = {
lastModified : content.LastModified,
bucketName : bucketId
}
if(!jQuery.isEmptyObject(bucketObj)){
csvBucketArr.push(bucketObj);
}
}
});
csv = csvBucketArr;
}
if(csvBucketArr.length!==0)
console.log("csvBucketArr :: ",csvBucketArr));
});
}(bucket.bucketName));
// console.log("Bucket " + index + " :: ", bucket);
});
},
You are pushing the same object into the array in each iteration of: 您将在以下每次迭代中将同一对象推入数组:
data.Contents.forEach(function(content,contentIndex){...});
So, as many times as that .forEach()
loop iterates, you end up pushing the exact same bucketObj
object into the csvBucketArr
array. 因此,
.forEach()
循环的多次迭代,您最终将完全相同的bucketObj
对象推入csvBucketArr
数组。
If you want each iteration of that .forEach()
to put a new and different bucketObj
object into the array, then you need to create a new object each time inside that loop like this: 如果您希望该
.forEach()
每次迭代将一个新的和不同的bucketObj
对象放入数组中,则每次在该循环内都需要创建一个新对象,如下所示:
sortBucket: function(bucketList) {
var counter, j = 0;
var str = "aws-billing-csv";
console.log("Bucket List :: ", bucketList);
bucketList.forEach(function(bucket, index) {
(function(bucketId) {
// console.log("Bucket Id :: ",bucketId);
s3Client.listObjects(params = {Bucket: bucketId }, function(err, data) {
var csvBucketArr = [];
if (err) {
document.getElementById('status').innerHTML = 'Could not load objects from ' + bucketID;
}
else{
//console.log("Bucket Data "+index+" :: ",data);
data.Contents.forEach(function(content,contentIndex){
// ===> create new bucketObj object
var bucketObj = {};
var fileKey = content.Key;
if(fileKey.search(str) != -1) {
// console.log("fileKey["+bucketId+"] "+contentIndex+" :: ",fileKey + " Date :: " ,content.LastModified);
bucketObj[fileKey] = {
lastModified : content.LastModified,
bucketName : bucketId
}
if(!jQuery.isEmptyObject(bucketObj)){
csvBucketArr.push(bucketObj);
}
}
});
csv = csvBucketArr;
}
if(csvBucketArr.length!==0)
console.log("csvBucketArr :: ",csvBucketArr));
});
}(bucket.bucketName));
// console.log("Bucket " + index + " :: ", bucket);
});
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.