let history = wx.getStorageSync('history') || [],
{ goods_id } = this.data.goodsInfo,
arr = [],//当前浏览的商品,为空则表示当前商品是第一次浏览
curIndex = 0 // 商品在 history 中的索引
// 筛选历史记录中是否浏览过该商品
history.filter(function (item, index) {
if (item.goods_id == goods_id) {
arr.push(item)
curIndex = index
}
})
if (!arr[0]) {
// 为浏览过则添加在数组头部
history.unshift(this.data.goodsInfo)
} else {
// 浏览过删除本条记录并添加在头部
history.splice(curIndex, 1)
history.unshift(arr[0])
}
wx.setStorageSync('history', history)
js筛选数组是否存在某个对象
最新推荐文章于 2025-05-13 16:46:04 发布