0

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

7
  • those inputs are from JSON-formatted data? Commented Apr 6, 2022 at 10:23
  • 1
    Have you tried anything? Like reading CSV format in JS, reading content line by line and split on spaces? Commented Apr 6, 2022 at 10:23
  • can you please share how this data is coming in the response? Is it an array or it is in the object and how do you want the result in array or object? Commented Apr 6, 2022 at 10:24
  • No..those are only numbers.. Commented Apr 6, 2022 at 10:25
  • @Ashishgoodluckchuck Can you add the piece of code that is returning this response? Commented Apr 6, 2022 at 10:27

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.