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

260
Visualizações
create array for each same month in javascript

i have got an response with number of values with same month.

If you can see the left one (TAR_HEF_01,TAR_HEF_02...)are the months and right one are the values for the same.

in this each month (lets say TAR_HEF_01) contain 4 values.

i have 6 months in a array.i have got monthIndexValue through loop like below.

hebrewMonthArray.forEach((monthValue, index) => {
let monthIndex = hebrewMonthArray.indexOf(hebrewMonthArray[index]);
      monthIndexValue =
        monthIndex < 9
          ? `TAR_HEF_0${monthIndex + 1}`
          : `TAR_HEF_${monthIndex + 1}`;

i have got value like below

Object.keys(allWorkDetails).forEach((work, index) => {
let value = allWorkDetails[work][monthIndexValue][0] || 0;
 }); 
});     

**Here is the console of monthIndexValue and value **:---

TAR_HEF_01 0
TAR_HEF_01 0
TAR_HEF_01 0
TAR_HEF_01 0
TAR_HEF_02 0
TAR_HEF_02 0
TAR_HEF_02 0
TAR_HEF_02 0
TAR_HEF_03 0
TAR_HEF_03 0
TAR_HEF_03 0
TAR_HEF_03 0
TAR_HEF_04 0
TAR_HEF_04 0
TAR_HEF_04 0
TAR_HEF_04 0
TAR_HEF_05 0
TAR_HEF_05 0
TAR_HEF_05 0
TAR_HEF_05 0
TAR_HEF_06 90
TAR_HEF_06 9
TAR_HEF_06 -200.8
TAR_HEF_06 214.9

i want to create one array for each month contain their values.

if i will put console(monthIndexValue,arrayOfValue);

the output should be:---

TAR_HEF_01 [0,0,0,0]
TAR_HEF_02 [0,0,0,0]
TAR_HEF_03 [0,0,0,0]
TAR_HEF_04 [0,0,0,0]
TAR_HEF_05 [0,0,0,0]
TAR_HEF_06 [90,9,-200.8,214.9]

How can i solve? Thanks...

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

0

If the response is a multiline string like you showed in your question, let's put its content in the response var like this:

const response = `TAR_HEF_01 0
TAR_HEF_01 0
TAR_HEF_01 0
TAR_HEF_01 0
TAR_HEF_02 0
TAR_HEF_02 0
TAR_HEF_02 0
TAR_HEF_02 0
TAR_HEF_03 0
TAR_HEF_03 0
TAR_HEF_03 0
TAR_HEF_03 0
TAR_HEF_04 0
TAR_HEF_04 0
TAR_HEF_04 0
TAR_HEF_04 0
TAR_HEF_05 0
TAR_HEF_05 0
TAR_HEF_05 0
TAR_HEF_05 0
TAR_HEF_06 90
TAR_HEF_06 9
TAR_HEF_06 -200.8
TAR_HEF_06 214.9`;

//now split the lines and put them in splitLines array
let splitLines = response.split(/\r?\n/);
let map = [];

//loop through each line    
splitLines.forEach( (o, i) => {
  //parse the string to isolate month and value
  let parsed = o.split(" ");
  let parsedMonth = parsed[0];
  let parsedValue = parsed[1];
  //se map still doesn't contain the key parsedMonth
  if( typeof map[parsedMonth] === 'undefined' ){
    //create a new empty array bound to the key parsedMonth in map
    map[parsedMonth] = [];
  }
  //add the new value to the array bound to the parsedMonth in map
  map[parsedMonth].push(parsedValue);  
});

At this point you have map with keys having values grouped by month.

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