Reputation: 55
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...
Upvotes: 0
Views: 119
Reputation: 8153
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.
Upvotes: 2