0

This is the continuation of this question which is here How to group by dynamic key value of an object of arrays(Month and year) Here i am getting output as expected but i want a small change on the output. The out put which is producing right now is as follows. enter image description here

But the result which i am expecting is goes like this.

enter image description here Code which i am using is like this.

           //Stack to retrive result
              let filteredStack:any =[];
            //Sorting based on the sorted key
              sortedKeys.forEach(k => {
                filteredStack.push(k,result[k]);
              });

How can i do this?

xxoyx
  • 81
  • 8
  • `filteredStack[k] = result[k]`. – Tim Roberts Mar 13 '22 at 05:49
  • @TimRoberts It is producing correct out put in console but it is not returning any value if i use in at this way. `sortedKeys.forEach(k => {filteredStack[k] = result[k];//filteredStack2.push(filteredStack);console.log(filteredStack);});return Promise.resolve(filteredStack);` – xxoyx Mar 13 '22 at 06:06
  • You are using filteredStack as a list/array type but wanting it to output like a map/object type. If you are trying to iterate over the array in this case it won't have anything in its indices. Because javascript treats everything like objects you can add arbitrary keys to an array like is suggested but it won't be array-like in functionality. You need to change the type to an object/map if that is what you want and use `for .. in` or something similar to iterate over the values. – lemieuxster Mar 13 '22 at 06:20
  • just to clarify you wish to sort by keys, correct ? – Saksham Khurana Mar 13 '22 at 06:29
  • Yes I wish to sort keys – xxoyx Mar 13 '22 at 06:35
  • @lemieuxster I have used the code like this `for (const k in result) { //console.log(`${k}: ${result[k]}`); filteredStack[k] = result[k]; console.log(filteredStack); } return Promise.resolve(filteredStack);` In console it is giving correct value but it is still not returning. – xxoyx Mar 13 '22 at 07:02

0 Answers0