Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

178
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda