0

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?

pilchard
  • 12,414
  • 5
  • 11
  • 23
  • Please fix the format of your question. There's text in the code block. Don't use bold text for inline code. Wrap it in `\`` -> [How do I format my posts using Markdown or HTML?](https://stackoverflow.com/help/formatting) – Andreas Nov 26 '21 at 16:24
  • 1
    [Is JavaScript a pass-by-reference or pass-by-value language?](https://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language) – Andreas Nov 26 '21 at 16:27
  • Try removing `this.` You should also avoid using square bracket notation for these properties. For example: `this.attachmentLinks['pdf'].push(this.attachmentObj);` should be `attachmentLinks.pdf.push(attachmentObj);` – dj11223344 Nov 26 '21 at 16:31
  • 1
    @dj11223344 _"You should also avoid using square bracket notation for these properties"_ - Why? There's no reason to not use bracket notation. – Andreas Nov 26 '21 at 16:39
  • @Andreas It's an anti-pattern. What purpose does bracket notation serve here? All the property names would be valid with dot notation and the code would look much cleaner. – dj11223344 Nov 26 '21 at 16:48
  • @dj11223344 Please add a reference for that _"it's an anti-pattern"_ statement. _"What purpose..."_ - Maybe OP likes that version more than dot notation. _"would be valid with dot notation"_ - Sure, but that doesn't make it the only valid and working option. _"would look much cleaner"_ - That's your opinion. OP might think different. – Andreas Nov 26 '21 at 16:51
  • @Andreas This has nothing to do with OPs question. There are several references in this post: https://stackoverflow.com/questions/4968406/javascript-property-access-dot-notation-vs-brackets OP can do whatever they want. It's advice, not scripture. Clean code is important when working in a team environment. – dj11223344 Nov 26 '21 at 17:12

0 Answers0