Here is a problem: Write a function that takes imageType and imageUrl and updates an object with fileName as key(value array) storing respective imageUrls on calling.
var attachmentLinks = {
pdf: [],
docx: [],
png: [],
jpg: [],
xlsx: [],
pptx: [],
other: [],
};
var attachmentObj = {
imgUrl: '',
isDuplicate: false,
duplicateCount: 0,
};
function addAttachmentToList(type, imgUrl) {
let fileType = type.toLowerCase();
this.attachmentObj['imgUrl'] = imgUrl;
switch (fileType) {
case 'pdf':
this.attachmentLinks['pdf'].push(this.attachmentObj);
break;
case 'docx':
case 'doc':
this.attachmentLinks['docx'].push(this.attachmentObj);
break;
case 'png':
this.attachmentLinks['png'].push(this.attachmentObj);
break;
case 'jpg':
case 'jpeg':
case 'jfif':
this.attachmentLinks['jpg'].push(this.attachmentObj);
break;
case 'xlsx':
case 'csv':
this.attachmentLinks['xlsx'].push(this.attachmentObj);
break;
case 'pptx':
this.attachmentLinks['pptx'].push(this.attachmentObj);
break;
default:
this.attachmentLinks['other'].push(this.attachmentObj);
break;
}
}
I am trying here to call function addAttachmentToList. The strange thing happening here is that on calling the function more than one time it is updating all other keys in object attachmentLinks that previously recieved data, with the data recieved in last(latest) switch case. AND if I am try to push any string(say imgUrl parameter) instead of object(attachmentObj) in the respective arrays in attachmentLinks it works fine. Can anyone help and explain why is this happening?