前言:
????????之最近在開發(fā)一個系統(tǒng)的時候,this.setData總有時候會出問題,查了查資料,一下進行總結(jié):
- 字段賦值;
-
數(shù)組靜態(tài)賦值;
-
數(shù)組動態(tài)賦值;
-
對象數(shù)組動態(tài)賦值;
-
組對象進行操作
? ? ? ? ① 末位加數(shù)據(jù);
? ? ? ? ② 中間或者開頭加數(shù)據(jù);
? ? ? ? ③?刪除數(shù)據(jù);
直接看代碼:
現(xiàn)在data定義:
testText : '',
testTextArray : ['A','B','C','D','E'],
testTextArrayOfObject : [
{
title : 'title_1',
content : '舞臺上的劉德華~劉德華~',
},
{
title : 'title_2',
content : '你是我的觀音菩薩,我是你的劉德華',
},
{
title : 'title_3',
content : '再看一眼就會爆炸哦~',
},
{
title : 'title_4',
content : '衣服包包給你換新的',
},
],
在onLoad操作:
????????
- 字段賦值;
//字段賦值 this.setData({ testText : 'abc' }) console.log("賦值過后的testText : ", this.data.testText);
-
數(shù)組靜態(tài)賦值;
//數(shù)組靜態(tài)賦值1 this.setData({ 'testTextArray[0]' : 'Xxx' }) console.log("數(shù)組靜態(tài)賦值過后的testTextArray1 : ", this.data.testTextArray); //數(shù)組靜態(tài)賦值2 this.setData({ ['testTextArray[1]'] : 'XXXX' }) console.log("數(shù)組靜態(tài)賦值過后的testTextArray2 : ", this.data.testTextArray);
-
數(shù)組動態(tài)賦值;
//數(shù)組動態(tài)賦值1: for(var i = 0 ; i < this.data.testTextArray.length ; i ++){ this.setData({ ['testTextArray[' + i + ']'] : 'X' }) } console.log("數(shù)組動態(tài)賦值1過后的testTextArray : ", this.data.testTextArray); //數(shù)組動態(tài)賦值2: for(var i = 0 ; i < this.data.testTextArray.length ; i ++){ let content = 'testTextArray[' + i + ']'; this.setData({ [content] : 'Y' }) } console.log("數(shù)組動態(tài)賦值2過后的testTextArray : ", this.data.testTextArray);
?然后在調(diào)試器里面檢查:
????????5. 組對象進行操作
? ? ? ? ????????① 末位加數(shù)據(jù);
//數(shù)組對象進行操作 -- 末位加數(shù)據(jù)
let testArray = {title : '敬請期待!',content : '新神誕生~'};
this.setData({
testTextArrayOfObject : this.data.testTextArrayOfObject.concat(testArray)
})
console.log("數(shù)組對象進行操作 -- 末位加數(shù)據(jù) 過后的testTextArray : ", this.data.testTextArrayOfObject);
? ? ? ? ????????② 中間或者開頭加數(shù)據(jù);
//數(shù)組對象進行操作 -- 開頭加數(shù)據(jù)
let testArray_1 = [{title : '敬請期待!',content : '新神誕生~'}];
this.setData({
testTextArrayOfObject : testArray_1.concat(this.data.testTextArrayOfObject)
})
console.log("數(shù)組對象進行操作 -- 末位加數(shù)據(jù) 過后的testTextArray : ", this.data.testTextArrayOfObject);
? ? ? ? ????????③?刪除數(shù)據(jù);
//數(shù)組對象進行操作 -- 刪除數(shù)據(jù)
let index = 0;
this.data.testTextArrayOfObject.splice(index , 1);//第一個是要刪除數(shù)據(jù)的索引,第二個是需要刪除數(shù)據(jù)的數(shù)量
console.log("數(shù)組對象進行操作 -- 刪除數(shù)據(jù) 過后的testTextArray : ", this.data.testTextArrayOfObject);
然后在調(diào)試器里面檢查:
文章來源:http://www.zghlxwxcb.cn/news/detail-719584.html
?文章來源地址http://www.zghlxwxcb.cn/news/detail-719584.html
到了這里,關(guān)于【微信開發(fā)】小程序this.setData({})中對普通字段、數(shù)組以及數(shù)組對象進行賦值,對數(shù)組對象進行操作的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!