-2

I want to take a field from nested JSON object as a string which is used as parameter.

I'm using a github project https://github.com/bencripps/react-redux-grid.

this react component is used for tree-grid.

I have a data and I want to map it as a tree-grid view.

My nested JSON object is below. It comes from an API. I want to take "2020.total" as a parameter (line 4)

 "id": 1,
 "parentId": -1,
 "Name": "Category 1",
 "2020": {total:120, tax:12},
 "children": [{
                "id": 11,
                "parentId": 1,
                "Name": "Category 11",
                "2020":  {total:23, tax:2},
               
            }]

when i finally get this parameter i use it as a variable in below code (line 16)

  const complexData = {
    showTreeRootNode: false,
    data:datafromAPI,
    gridType: 'tree',
    dragAndDrop: true,
    columns: [
        {
            name: 'Name',
            className: 'additional-class',
            dataIndex: 'Name',
            sortable: false,
            expandable: true
        },
        {
            name: 'Total Payment',
            dataIndex: 2020.total,
            sortable: false,
            className: 'additional-class'
        }],
    
};

"Name" attribute is working fine but "Total Payment" is not working.

i tried those syntax;

'2020.total'
'[2020].total'
'[2020][total]'

I know this is a little bit syntax question but i don't know how to search for it.

thanks for all answer.

AhuraMazda
  • 460
  • 4
  • 22
  • Does this answer your question? [How can I access and process nested objects, arrays or JSON?](https://stackoverflow.com/questions/11922383/how-can-i-access-and-process-nested-objects-arrays-or-json) – Emile Bergeron Jul 22 '20 at 13:38

2 Answers2

0
let a = {
"id": 1,
"parentId": -1,
"Name": "Category 1",
"2020": {total:120, tax:12},
"children": [{
     "id": 11,
     "parentId": 1,
     "Name": "Category 11",
     "2020":  {total:23, tax:2},             
   }]
}

Try a[2020].total

Kid
  • 1,160
  • 2
  • 14
  • 31
  • thanks for answer, but there is no JSON obeject to attach a variable. there is a lot of nested obejct. I update the question. – AhuraMazda Jul 22 '20 at 12:25
  • @AhuraMazda but even if you receive data from an API, they are stored in a variable, not? What about `data:datafromAPI`? – AlexRekowski Jul 22 '20 at 12:36
  • ok, i use it as datafromAPI[2020].total what about children's 2020. it is not recognize total – AhuraMazda Jul 22 '20 at 12:38
  • 1
    Then you can use `datafromAPI.children[0]['2020'].total`. In your question you were asking about parameter from line 4, you didn't ask about children. It seems like your question is not complete, it's not clear what your end goal is. – AlexRekowski Jul 22 '20 at 12:48
  • it is not a one time operation. it is a loop. so i do not need only one nodes parameter. it should be reuseable variable. thanks for answer – AhuraMazda Jul 22 '20 at 14:26
0

You can access it like this:

let a = {
  "2020": {total:120, tax:12},
}

a[2020].total
a["2020"].total
Yash Joshi
  • 2,586
  • 1
  • 9
  • 18