Vuejs, how to build an object?

妖精的绣舞 提交于 2020-07-10 08:49:29

问题


Hello I'm trying to display graphics on my website from API, to display my graphic in column, I need to send him an object like this one

data: [{
            name: 'Data1',
            data: [
              ['ALIONIVS', 1],
              ['GARCIC(I)VS', 1],
              ['NONIVS', 1],
              ['SERVERS,A', 1]
            ],
          },
          {
            name: 'Data2',
            data: [
              ['TVTOR', 1],
              ['FVSCINVS', 1],
              ['GVTAMVS / GVMATIVS', 1],
              ['SEVERINVS, -A', 1],
              ['TVSCVS, -A / TVSGVS', 1],
              ['VEIENTA', 1],
            ]
          }
        ],

But I would like to have dynamic data I'm retrieving data from my api with axios.

        axios
          .get('../api/civitasnomen/' + this.searchInputcivitas)
          .then(response => {
            this.data1 = response.data.map(item => {
              return [item.lemme, item.nb];
            })
          })

        axios
          .get('../api/civitascognomen/' + this.searchInputcivitas)
          .then(response => {
            this.data2 = response.data.map(item => {
              return [item.lemme, item.nb];
            })
          })

In my object I would like to retrieve both my api data

If write this in axios :

axios .get('../api/civitasnomen/' + this.searchInputcivitas) .then(response => this.data1 = response.data )

For example Data1 =

[ { "nb": 1, "lemme": "ALIONIVS" }, { "nb": 1, "lemme": "GARIC(I)VS" }, { "nb": 1, "lemme": "NONIVS, -A" }, { "nb": 1, "lemme": "SEVERVS, -A" } ] 

And Data2 =

[ { "nb": 1, "lemme": "TVTOR" }, { "nb": 1, "lemme": "FVSCINVS" }, { "nb": 1, "lemme": "GVTAMVS / GVMATIVS" }, { "nb": 1, "lemme": "SEVERINVS, -A" }, { "nb": 1, "lemme": "TVSCVS, -A / TVSGVS" }, { "nb": 1, "lemme": "VEIENTA" } ]

回答1:


Suppose that you have data property initialized as an empty array:

data(){
  return {
   data: [],

  }
}

Then update it with result coming from API :

 axios
          .get('../api/civitasnomen/' + this.searchInputcivitas)
          .then(response => {
             this.data.push({
               name:'data1',
               data:response.data.map(item => {
              return [item.lemme, item.nb];
            })
            })
          })

        axios
          .get('../api/civitascognomen/' + this.searchInputcivitas)
          .then(response => {
             this.data.push({
               name:'data2',
               data:response.data.map(item => {
              return [item.lemme, item.nb];
            })
          })


来源:https://stackoverflow.com/questions/62531807/vuejs-how-to-build-an-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!