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

186
Visualizações
array push in javascript associative array

I am trying to create an associative array of dates and style. I am expecting an array like

{   
   dates: [{day: 1, month: 12, year: 2021}, {day: 15, month: 12, year: 2021}],
   styleClass: 'test'
},

And my code is

var markedDates = [];
markedDates['dates'].push('day: 1, month: 12, year: 2021');
markedDates['dates'].push('day: 15, month: 12, year: 2021');
markedDates['styleClass'].push('test');
console.log(markedDates);

which returns error

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Problem 1: markedDates has been defined as an array ([]). To define it as an object (which is what you have as your expected output), use {}. (No such thing as an 'associative array' in JavaScript; arrays use 0, 1, etc. as indexes. Objects use 0, 1, key, another_key as indexes [or keys, more accurately].)

Problem 2: You haven't defined the dates array within markedDates which will cause a reference error.

Problem 3: You are pushing strings to the array instead of objects. This won't error, but won't produce the intended output.

Problem 4: You're trying to use styleClass as an array - it's just a string property within the markedDates.

To fix your code in the line-by-line manner (keeping similarity to your original code):

var markedDates = {};

markedDates.dates = [];
markedDates.dates.push({day: 1, month: 12, year: 2021});
markedDates.dates.push({day: 15, month: 12, year: 2021});
markedDates.styleClass = "test";

console.log(markedDates);
.as-console-wrapper { max-height: 100% !important; top: auto; }

But if you've got your expected output statically defined at the top, you can just assign that directly to the variable as such:

var markedDates = {
  dates: [{ day: 1, month: 12, year: 2021 }, { day: 15, month: 12, year: 2021 }],
  styleClass: 'test'
};

console.log(markedDates);

about 4 years ago · Juan Pablo Isaza Relatório

0

Things that you should consider -

  1. Make an object that needs to be pushed into the bigger array. Eg: markedDate in the below code.
  2. Assigning a string should be directly with an equal to operator and no need to push it, like in an array
  3. Initialize the internal field dates as an array before pushing into it first

var markedDates = [];
var markedDate = {};
markedDate['dates'] = [];
markedDate['dates'].push({day: 1, month: 12, year: 2021});
markedDate['dates'].push({day: 15, month: 12, year: 2021});
markedDate['styleClass'] = 'test';
markedDates.push(markedDate);
console.log(markedDates);

about 4 years ago · Juan Pablo Isaza Relatório
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