0

I have a React component which is pulling JSON data via an axios.get call and then I am mapping an embedded object using the following function:

axios.get('MasterData.json').then(response => {
       const fullTree = response.data.graph.tree;
       const resultTree = Object.keys(fullTree).map(key => ({
          ...fullTree[key],
         id: key
    }));

This produces the following:

{5bd356cc-5ee6-49a0-ab68-65cbf0209105: Array(6), id: "5bd356cc-5ee6-49a0- ab68-65cbf0209105"}

which is great, but I need to add a label of "ports: to the entire nested array (Array(6)) that is being output above so that I can map it. Any help is appreciated.

3
  • 2
    You can use ({ ports : ...fullTree[key], id: key }) instead of ({ ...fullTree[key], id: key }) Commented Nov 5, 2018 at 13:09
  • What do you mean by 'add a label'? Can you add details on how the structure is now and what your expected output is? Commented Nov 5, 2018 at 13:20
  • Hassan I get a parsing error with your method (my first thought was to try that). Dinesh, what I am trying to do is exactly what I thought Hassan's code would do, which is give the entire array a key of "ports." Commented Nov 5, 2018 at 13:56

1 Answer 1

1
axios.get('MasterData.json').then(response => {
   const fullTree = response.data.graph.tree;  
   console.log(fullTree) // you must check this fist
   const resultTree = Object.keys(fullTree).map(key => 
     new Port({  // create some function to generate get port as you want
        port : fullTree[key],
        id: key
     )
});

for create function maybe on bellow me can explain more :)

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

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.