汪晓康

导航

微信小程序,操作数组

wxml

<!--pages/shanchu/shanchu.wxml-->
<view>
<input placeholder="please input number" bindinput="getnum"></input>
<button bindtap="add">增加</button>
<button bindtap="delete">删除</button>
<text>数组是:{{lst1}}</text>
</view>

js

// pages/shanchu/shanchu.js
var num1=""
Page({

  /**
   * 页面的初始数据
   */
  data: {
    lst1: [1, 2, 3, 4]
  },
  getnum: function (name) {
    num1 = parseInt(name.detail.value)
    console.log(num1)
  },
  add: function () {
    this.data.lst1.push(num1)
    console.log(this.data.lst1)
    this.setData({
      lst1: this.data.lst1
    })
  },
  delete() {
    var lst2 = this.data.lst1
    console.log(lst2)
    var lst3 = []
    for (var i = 0; i < lst2.length; i++) {
      if (lst2[i] != num1) {
        lst3.push(lst2[i])
      }
    }
    console.log(lst3)
    this.setData({
      lst1:lst3
    })
  }
})

posted on 2021-08-09 23:27  汪晓康  阅读(208)  评论(0编辑  收藏  举报