1.数据区声明变量
ruleForm: {
role:'',
sdemands:'',
demands: []
},
2.获得服务器返回数据(服务器端返回role,sdemands,没有demands数据),赋值this.ruleForm=...(这个没有问题)
具体需求如下:
sdemands数据形式如下:a(1),b(2)
想要将sdemands解析并赋值给demands,是的demands变量形式如下:
demands:[
{value1:a,value2:1},
{value1:b,value2:2}
]
3.操作如下,但是前段报错506 Uncaught TypeError: Cannot read property 'push' of undefined
var ded=this.ruleForm.sdemands.split(',');
var ded1='';
var ded2='';
for(var i=0;i<ded.length;i++){
ded1=ded[i].split('(')[0];
ded2=ded[i].split('(')[1];
ded2=ded2.substring(0,ded2.length-1);
this.ruleForm.demands.push({value1: ded1, value2: ded2, key: Date.now()});
}
补充一下:先声明数组test,赋值给test,然后test在复制给demands就好了,但是有没有更好的解决办法
var test=[];
var ded=this.ruleForm.sdemands.split(',');
var ded1='';
var ded2='';
for(var i=0;i<ded.length;i++){
ded1=ded[i].split('(')[0];
ded2=ded[i].split('(')[1];
ded2=ded2.substring(0,ded2.length-1);
test.push({value1: ded1, value2: ded2, key: Date.now()});
}
console.log(test);
this.ruleForm.demands=test;
console.log(this.ruleForm.demands);
RISEBY
相关分类