微信小程序data数组赋值

 //方法一
let array=[];
Page({
  data: {
  	dataArr: [],
   dayArr: [{ month: "11",day: "8", price: "11.00"},
      { month: "11", day: "9", price: "11.00"  },
      { month: "11", day: "10", price: "11.00"},
      { month: "11", day: "11", price: "11.00" },
      { month: "11", day: "12", price: "11.00"},
      { month: "11", day: "13", price: "11.00"},
      { month: "11", day: "14", price: "11.00" },
      { month: "11", day: "15", price: "11.00",},
      { month: "11", day: "16", price: "11.00",},
      { month: "11" ,day: "17", price: "11.00"},
      { month: "11", day: "18", price: "11.00" },
      { month: "11", day: "19", price: "11.00" },
      { month: "11", day: "20", price: "11.00"},
    ],
  },
  onShow: function () {
 
	let dayArr = this.data.dayArr;
    for(let i=0;i<dayArr.length;i++){
      let obj={};
      obj.month = dayArr[i].month;
      obj.day = dayArr[i].day;
      array.push(obj);
      this.setData({
        "dataArr": array
      });
     
    }
	}
  })
//第二种es6写法
Page({
  data: {
  	dataArrs: [],
   dayArr: [{ month: "11",day: "8", price: "11.00"},
      { month: "11", day: "9", price: "11.00"  },
      { month: "11", day: "10", price: "11.00"},
      { month: "11", day: "11", price: "11.00" },
      { month: "11", day: "12", price: "11.00"},
      { month: "11", day: "13", price: "11.00"},
      { month: "11", day: "14", price: "11.00" },
      { month: "11", day: "15", price: "11.00",},
      { month: "11", day: "16", price: "11.00",},
      { month: "11" ,day: "17", price: "11.00"},
      { month: "11", day: "18", price: "11.00" },
      { month: "11", day: "19", price: "11.00" },
      { month: "11", day: "20", price: "11.00"},
    ],
    addpeople:[{
      name:"",
      phone:"",
      idcard:"",
      passport:""
    }],
  },
  onShow: function () {
  //方法一
	let dayArr = this.data.dayArr;
    for(let i=0;i<dayArr.length;i++){
      let obj={};
      obj.month = dayArr[i].month;
      obj.day = dayArr[i].day;
      /*在this.data中设置dataArrs这条数据,在方法中,我们定义dataArrs变量让其等于that.data.dataArrs;
       *  然后对ceshi进行操作,其实就是对that.data.ceshi进行操作,
       而that.setData({dataArrs})就等同于that.setData({dataArrs : dataArrs }) ; 
       第一个ceshi 是that.data.ceshi, 第二个ceshi 是我们定义的变量ceshi。*/
      let dataArrs = this.data.dataArrs;
      dataArrs.push(obj)this.setData({dataArrs})
    }
    将key值以数据路径的形式赋值,可以达到修改数据中的某一条,
    this.setData({
    'list[0].title' : 'change data',   //数据路径key必须带''号
    'list[0].num' : 'change data'      //数据路径key必须带''号
	})
  	//  方法二
    usenames(e){
	    let value =e.detail.value;
	    let index = e.target.dataset.index;
	    let name = 'addpeople['+index+'].name';//获取到data里面的addpeople相对应的下标的值并赋值
	    this.setData({
	      [name]:value	 //用[]表示的变量
	    })
		}
 	 },
  })

//第二种es6写法
Page({
  data: {
  	
    addpeople:[{
      name:"",
      phone:"",
      idcard:"",
      passport:""
    }],
  },
  //  方法二
   usenames(e){
	 let value =e.detail.value;
	 let index = e.target.dataset.index;
	 let name = 'addpeople['+index+'].name';//获取到data里面的addpeople相对应的下标的值并赋值
	 this.setData({
	    [name]:value	 //用[]表示的变量
	  })
	}

将key值以数据路径的形式赋值,可以达到修改数据中的某一条,

  
    this.setData({
    'list[0].title' : 'change data',   //数据路径key必须带''号
    'list[0].num' : 'change data'      //数据路径key必须带''号
	})

你可能感兴趣的:(微信小程序)