温馨提示×

怎么在微信小程序中操作数组

养鱼的猫咪
1678
2021-04-21 11:58:17
栏目: 云计算
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在微信小程序中操作数组的方法:1.使用push()函数向数组添加数据;2.使用split()函数删除数组数据;3.使用setData方法修改数组数据;

怎么在微信小程序中操作数组

具体方法如下:

1.使用push()函数向数组添加一条数据

saveNote: function(e) {

var obj = e.detail. value;

var title = obj. title;

var detail = obj .detail;

if(tit1e.1ength<=0 || detail. 1ength<=0){

return;

}

this . data . notes. push(obj);

var update = this. data. notes;

console. log(update);

app. globalData.notes = update ;

wx. redirectTo({

url: ' . . /notes/notes '

})

}

2.使用split()函数删除数组中的数据

Wx. showModal({

title: '提示'

content:'是 否删除该纪录',

success:function(res){

if(res. . confirm){

var notes =that. data. notes;

notes. splice(id,1);

that. setData({

notes: notes

})

console .1og("删除成功");

}

}

})

3.使用setData方法修改数组中的数据

saveNote: function(e) {

var obj = e.detail. value;

var title = obj. title;

var detail = obj. detail;

if(tit1e.length<=0 || detail.length<=0){

return;

}

war that = this;

var notes =that . data. notes;

var id = that.data.id;

notes[id] = obj;

app. globalData.notes = notes;

WX . redirectToft({

url: ' . . /notes/notes '

})

}

})

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:怎么在微信小程序中对数组赋值

0