WeChat applet setData dynamically assigns values to arrays

need:

The business needs to dynamically assign values ​​to the array. The array code is as follows:

Page({

  /**
   * 页面的初始数据
   */
  data: {
    arr: []
  }
})

Not only to assign values ​​to the array, but also to update to the page display, the setData() function is used here.

Solution:

code:

addArr() {
    var that = this;
    var param = {};
    var string = "arr[" + that.data.arr.length + "]";
    param[string] = that.data.arr.length + 1;
    that.setData(param);
    console.log("arr-> ", that.data.arr);
  }

I'm not sure if there are any elements in the array, or how many elements there are in the array. Here, the length of the array is used as the subscript of the element to be added to the array. Then add it.

Effect:

=============== end ===============

Guess you like

Origin blog.csdn.net/javaXiaoAnRan/article/details/105161465