0

I have the following:

  children: [
    {
      name: 'test',
      path: 'test',
      meta: {
        label: 'test',
        link: 'test'
      },
      component: lazyLoading('test/basic')
    },

    {
      name: 'test',
      path: 'test',
      meta: {
        label: 'test',
        link: 'test'
      },
      component: lazyLoading('test/Basic')
    },

    {
      name: 'test',
      path: 'test',
      meta: {
        label: 'test',
        link: 'test'
      },
      component: lazyLoading('test/Basic')
    }
  ]

I want to follow this structure, but programmatically create each dictionary using each record returned from an API call.

example api call

function getData() {
    axios.get(Url + input.value)
    .then(function (response) {
        json_data = response.data;
    });
}

so in python this would probably look something like:

test_list=[]

for item in json_data:
    dict = {
           name: item.name
           path: item.path
           meta:  {
             label: item.label,
             link: item.link,
                  },
           component: lazyLoading('testitem/basic')
           },
     test_list.append(dict)
children: test_list

How can I accomplish this in javascript? I'm having a real hard time learning javascript after doing python.

UPDATE:

This is the full code block that I am working with.

export default {
  name: 'test',
  meta: {
    icon: 'fa-android',
    expanded: false
  },

  children: [
    {
      name: 'test',
      path: 'test',
      meta: {
        label: 'test',
        link: 'test'
      },
      component: lazyLoading('test/Basic')
    }
  ]
}

1 Answer 1

1

You were very close:

const children = [];
json_data.forEach(item => {
    const dict = {
       name: item.name,
       path: item.path,
       meta:  {
           label: item.label,
           link: item.link,
       },
       component: lazyLoading('testitem/basic'),
    }
    children.push(dict);
});
Sign up to request clarification or add additional context in comments.

5 Comments

so are you pushing each dictionary to 'children' which is a list?
Yes, the list (array) is initialized before the loop and on each iteration I just put the newly formed dict (object) into it. Is not what you need?
in the OP, is children a list? It looks like children is a key and the list is the value? Maybe in JS this is the same thing as children = list?
It's not clear from your code what children belongs to. You can't just do children: test_list, the colon means that it's a key-value pair in an object. If it is so, then you can initialize it like this: children: [] and then push items with obj.children.push(item).
I made a new post because I realize that my question was incomplete and I did not provide enough information. stackoverflow.com/questions/48855114/…

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.