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)
}
}
}

}

猜你喜欢

转载自blog.csdn.net/liulei21/article/details/80907745
今日推荐