Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

179
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda