SoFunction
Updated on 2025-04-11

How to change a string into an array

vue turns string into array

var li=(',')
      //Get the index      var index=(e);
      if(index>-1){
        //Delete elements in the array        (index, 1)
      }
      //Convert array to string      =(',')
      //If the array is empty, hide i      if(==0){
        =''
      }

In vue, get request pass parameter is an array and becomes a string

question

In the vue project, when using axios to encapsulate request data, when the get request passes the parameters as an array, the get splicing url cannot pass the parameters correctly.

solve

Get parameter transmission needs to be transferred by yourself in the first step.

  • Initial error transmission
let provinces= ['1', '2', '3']
('/gateway/xxx', {
    params: {
      ID: 001,
      provincesData: provinces,
    }
  })
// The above request is '.../user?ID=001&provincesData[]=1&provincesData[]=2&provincesData[]=3

This will happen if get directly passes array

  • Convert parameter transfer format
('/gateway/xxx', {
    params: {
      ID: 001,
      provincesData: provinces+ '',
    }
  })
// Add ( + '') to the parameter to turn the array into a string//.../user?ID=001&provincesData=1,2,3

Turning an array into a string in JavaScript: array [arr] + ’ ’

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.