JS把字符串变为我想要的数组

314 阅读1分钟

需求

element的上传组件upload其中有一个的属性,而我们的需求就是通过这个属性把需要编辑的商品原有图片战术出来可能是N张

:file-list="imgList"

我需要的格式是:

imgList:[
    {url:''xxx},
    {url:''xxx},
    {url:''xxx},
    {url:''xxx},
    ...
]

后端传个我的数据是:

imgList:'https://static.mdaren.cn/test2/photoUrl20210511134901.jpg,
https://static.mdaren.cn/test2/photoUrl20210511134904.jpg,
https://static.mdaren.cn/test2/photoUrl20210511134907.jpg'

解决

我这边需要做的处理:

//设定传给我的数据是data
const item= data.split(',')//split(',')可以把这个字符串以','为分割
for(i in item){
    console.log(i)//此时打印出来的i是key
    imgList.push({url:item[i]})//item[i]才是我们想要的value
}

此时当门面打卡页面时,原有的图片列表才能正常显示 image.png