0

I am getting below response from backend(NodeJS),

current Response:

 [ [ '49' ], [ '33' ], [ '60' ], [ '58' ] ]

Need to pass these above values to Highchart component in JSON Object format as like below in my frontend -Angular 9. I am facing issues in converting two dimensional array values to its corresponding json format.

Here key 'fin' and its values should be static. For key -"A" , i need array element 1,3,.. and for key -"B" i need array elements 2,4,etc .

Can anyone guide me , Please.

Required format:

{
"fin":[
       '14',
       '16'
      ]
"A" : [
       '49',
       '60'
      ]
"B" : [
       '33',
       '58'
      ]
}
3
  • if there is more than one element inside array at (0) position, say[['49, '2nd_ele'], [],...], should the output show "A": ['49', '2nd_ele', '60'] ? Commented Jul 7, 2020 at 10:30
  • Pleas post what you have tried and some code snippet. SO does not programming service. Commented Jul 7, 2020 at 10:32
  • @Chandra. I will get only one array element (position 0) always inside inner array.. Commented Jul 7, 2020 at 11:49

1 Answer 1

1

One way to go about it, would be to, segregate the currentResponse array as per need.

listA = [];
listB = [];
for (i = 0; i < currentResponse.length; i++) {
  if(i%2==0) listA.push(currentResponse[i][0]);
  else listB.push(currentResponse[i][0]);
}

Once segregated, we can build the JSON as a simple JS Object

jsonObj = {
  fin : [
       '14',
       '16'
      ]
  A : listA
  B : listB
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Chandra.. Thanks for your answer .. Its worked !..

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.