Page({
data: {
helloWorld: 'hello!',
people: [{
name: "张三",
age: 20
}, {
name: "李四",
age: 18
}, {
name: "赵五",
age: 35
}]
},
onLoad: function(options) {
var that = this;
console.log('改变值前')
console.log('helloWorld:'+that.data.helloWorld)
console.log('people[0].age:' + that.data.people[0].age)//输出第一个人张三的年龄
that.setData({
helloWorld:'你好!',
'people[0].age':25//修改年龄为25,数组对象赋值需要用''包裹
})
console.log('改变值后')
console.log('helloWorld:' + that.data.helloWorld)
console.log('people[0].age:' + that.data.people[0].age)//输出第一个人张三的年龄
}
})
但在实际使用中,我们对数组的中的某个元素的设置是动态的。即 我们通常应该是 ‘people[‘+index+’].age’ : ‘changed number’ ,其中index应该是一个动态的数字。但显然,这样是无法使用在对象的key中的。
所以,只能使用了一个变通的方法点击转跳参考这篇博客