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

257
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
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