level 1
忧郁的刀疤男27
楼主
有一个字符串数组必要把需的字符串进行分割后赋值给对象的值,有没有什么更高效的方法?
const strs=['1 张三 广州','2 李四 四川'] //需要分割的字符串数组
const userArr=[] //存储对象数组
function UserInfo(id,name,address){
this.id=id,
this.name=name,
this,address=address
} //构造函数
for(let i=0;i<strs.length;i++){
const userInfo = new UserInfo()
const user= strs[i].split(' ') //对数字里的字符串进行分割
userInfo.id= user[0]
userInfo.name = user[1]
userInfo.address = user[2]
userArr.push(userInfo)
}
有没有更好的方法,最好是不要用userInfo.id = user[0]这样指定下标赋值的。。求大神解答
2023年07月01日 00点07分
1
const strs=['1 张三 广州','2 李四 四川'] //需要分割的字符串数组
const userArr=[] //存储对象数组
function UserInfo(id,name,address){
this.id=id,
this.name=name,
this,address=address
} //构造函数
for(let i=0;i<strs.length;i++){
const userInfo = new UserInfo()
const user= strs[i].split(' ') //对数字里的字符串进行分割
userInfo.id= user[0]
userInfo.name = user[1]
userInfo.address = user[2]
userArr.push(userInfo)
}
有没有更好的方法,最好是不要用userInfo.id = user[0]这样指定下标赋值的。。求大神解答
