Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

177
Views
Javascript: Use switch statement to push an object in an array

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?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!