1

I have a React component which is access JSON data for populate a tree component. The tree is showing nodes and ports. Here is a sample from the JSON:

"outputs": {
        "graph": {
            "nodes":[ {
                "name":"nlabme3400",
                "ports":[ {
                    "name": "GigabitEthernet 0/2", "id": "5bd350c7-d15b-4f8b-be70-18eda2bfe41a"
                }
                ,
                {
                    "name": "FastEthernet 0/19", "id": "5bd350c7-762d-4462-984b-e6f0a9edb6c7"
                }
                ,
                {
                    "name": "FastEthernet 0/21", "id": "5bd350c7-2927-43db-ae43-119b12636de6"
                }
 ],
                "id":"5bd350bf-8515-4dc2-9b12-16b221505593"
            }

I have all of this information coming in to my component via the following axios get call:

  axios.get('StepThreeFinalData.json').then(response => {
           const nodess = response.data.outputs.graph.nodes.map(({id, name, 
...children}) => ({value: id, label: name, children: children.ports}));

The output is working perfectly. However, the challenge is that I need to change the "name" and "id" tags in the children array to "label" and "value", respectively, because otherwise the label will not show up in the tree component. Not sure how to do this. Please help!

3
  • What is wrong with your current code ? Commented Nov 1, 2018 at 15:17
  • your example json does not have children. did you mean ports? Commented Nov 1, 2018 at 15:17
  • 1
    @HMR children is constructed with the spread syntax so it's just the other key: values of the destructured object Commented Nov 1, 2018 at 15:19

3 Answers 3

1
const ports = [ {
                    "name": "GigabitEthernet 0/2", "id": "5bd350c7-d15b-4f8b-be70-18eda2bfe41a"
                }
                ,
                {
                    "name": "FastEthernet 0/19", "id": "5bd350c7-762d-4462-984b-e6f0a9edb6c7"
                }
                ,
                {
                    "name": "FastEthernet 0/21", "id": "5bd350c7-2927-43db-ae43-119b12636de6"
                }
 ]

 const update_ports = (ports) => ports.map(({ id, name }) => {
   return { label: name, value: id }
 })
console.log(update_ports(ports)) // The new ports with the new keys and values.

You can use the map function and return new array of objects with new keys and values in each item in the array.

axios.get('StepThreeFinalData.json').then(response => {
           const nodess = response.data.outputs.graph.nodes.map(({id, name, 
...children}) => ({value: id, label: name, children: update_ports(children.ports)}));

Notice i've called to update_ports in your axios success.

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

1 Comment

@CHays412, You are welcome. Kindly ask to accept the answer whether it solved your issue (Clicking the Accept answer button) Good luck !
0
const test = {
  "outputs": {
    "graph": {
      "nodes": [{
        "name":"nlabme3400",
        "ports": [
          {
            "name": "GigabitEthernet 0/2", "id": "5bd350c7-d15b-4f8b-be70-18eda2bfe41a"
          },
          {
            "name": "FastEthernet 0/19", "id": "5bd350c7-762d-4462-984b-e6f0a9edb6c7"
          },
          {
            "name": "FastEthernet 0/21", "id": "5bd350c7-2927-43db-ae43-119b12636de6"
          }
        ],
        "id":"5bd350bf-8515-4dc2-9b12-16b221505593"
      }]
    }
  }
};

const test2 = test.outputs.graph.nodes.map(({name, ports, id}) => ({
  name,
  id,
  ports: ports.map(({name, id}) => ({
    label: name,
    value: id
  }))
}));

console.log(test2);

Read more about map, filter, reduce, that will save your life

Comments

0

axios.get('StepThreeFinalData.json').then(response => { const nodess = response.data.outputs.graph.nodes.map(({id, name, ...children}) => ({value: id, label: name, children: update_ports(children.ports)}));

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.