RN中实现个人中心中 浏览足迹

本文介绍了一种基于用户浏览行为的足迹管理系统实现方法,重点在于如何通过数组索引记录不同类型的浏览信息,并提供了具体实现代码示例,包括如何判断用户浏览记录是否存在、如何添加新的浏览记录等。

我的数组设计(因为我项目中有几个分类,如果是一类商品把type去掉,只存的浏览数组的索引)

browse: [{ type: '附近棋牌', content: [{ index: 2, index2: 0 }] },
{ type: '精彩图片', content: [{ index: 1, index2: 0 }, { index: 0, index2: 0 }] },
{ type: '资讯', content: [{ index: 0, index2: 0 }, { index: 1, index2: 1 }] },
{ type: '直播', content: [{ index: 2, index2: 1 }, { index: 0, index2: 1 }] }],

下边以资讯为例详细介绍一下

一、这是浏览足迹页面的逻辑,显示可以根据自己的需求编写,,,遍历zixun()返回的数组就好了

found = () => {
for (let i = 0; i < t('userinfo').length; i++) {
if (this.state.username === t('userinfo')[i].username) {
//通过query()方法获取登录的用户名进行判断
this.setState({
panduan: true,
})
return t('userinfo')[i].browse

}
}
}//判断有没有找到用户
founds = () => {
for (let i = 0; i < t('userinfo').length; i++) {
if (this.state.username === t('userinfo')[i].username) {
return t('userinfo')[i].browse

}
// else { }

}
}//返回该用户的浏览数组
zixun = () => {
const arr = []
t('zixun').map((item, Fistindex) => {
return (
this.state.panduan ? this.founds().map((ite, ind) => {
return (
ind === 2 ?
ite.content.map((it, ind) => {
return (
Fistindex === it.index ?
item.contentss.map((itm, inds) => {
return (
inds === it.index2 ?
arr.push(itm) : null
)
})
: null
)
})
// console.log(ite)
: null
)
}) : null
)
})
return arr
}
//把通过索引把资讯对象放在一个临时数组中

二、资讯详情页面的逻辑

found() {
for (let i = 0; i < t('userinfo').length; i++) {
if (this.state.username === t('userinfo')[i].username) {
return t('userinfo')[i].browse
}
else { }
}
}

panduanB = () => {
for (let i = 0; i < this.found().length; i++) {
if (this.found()[i].type === t('tab2')) {//通过type判断
for (let j = 0; j < this.found()[i].content.length; j++) {
if (this.found()[i].content[j].index === this.props.navigation.state.params.index) {
this.setState({
pan: j//如果存在记录下标,有助于删除
})
return false
}
}
}
}
return true
}
tianjia = () => {
for (let i = 0; i < this.found().length; i++) {
if (this.found()[i].type === t('tab2')) {
if (this.panduanB() === true) {
obj = {
index: this.props.navigation.state.params.index1,
type: t('tab2'),
index2: this.props.navigation.state.params.index
}
this.found()[i].content.unshift(obj)
//没有直接添加
// alert('成功添加')
// console.log('成功添加', this.found()[i].content.length)
}
else {
obj = {
index: this.props.navigation.state.params.index1,
type: t('tab2'),
index2: this.props.navigation.state.params.index
}
// alert('之前存在') 如果存在,先把之前的删除,再在头部添加
this.found()[i].content.splice(this.state.pan, 1)
this.found()[i].content.unshift(obj)
}
}
}

}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值