微信小程序数据处理01-JSON复杂对象处理
- 通常返回回来的数据是这样的:
- 里面还可能有这些数据:
- 当然这些才是我们真正要处理的数据(主要是对多张图片进行的数据处理)
前情提要大概是这些喽,
需求:有些banner可能有多张图片,用逗号分隔,需要对里面的数据进行处理,并修改源数据内容
开始吧 !
response: ({//这里是对后端返回回来的数据进行处理,解构了一个data
data
}) => {
var bannerArr = data.records
bannerArr.forEach((item, index) => {
let banner = item.banner
if (banner.indexOf(',') != -1) { // 判断当前 banner 字符串是否含有 **,** ,有则进行数据处理,当然也可以通过其他方法,比如 has()等
console.log(index) // 这里的 index 其实是当前item 所对应的 index(下标),表明当前item 所在位置
data.records[index].banner = banner.split(',')
console.log(banner.split(","))
} else {
// console.log(index)
data.records[index].banner = banner
}
})
// 不确定的地方一定要断点或打印
// console.log(bannerArr)
this.setData({
noteImgList: data
})
}
目标达成啦,遇事不慌呐😀