js 操作数据方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import storage from 'good-storage'
 
function inertArray(arr, val, compare, maxLen) {
  const index = arr.findIndex(compare)
  if (index === 0) {
    return
  }
  if (index > 0) {
    arr.splice(index, 1)
  }
  arr.unshift(val)
  if (maxLen && arr.length > maxLen) {
    arr.pop()
  }
}
 
function deleteFromArray(arr, compare) {
  const index = arr.findIndex(compare)
  if (index > -1) {
    arr.splice(index, 1)
  }
}
 
export function save(item, key, compare, maxLen) {
  const items = storage.get(key, [])
  inertArray(items, item, compare, maxLen)
  storage.set(key, items)
  return items
}
 
export function remove(key, compare) {
  console.log(compare)
  const items = storage.get(key, [])
  deleteFromArray(items, compare)
  storage.set(key, items)
  return items
}
 
export function load(key) {
  return storage.get(key, [])
}
 
export function clear(key) {
  storage.remove(key)
  return []
}
 
export function saveAll(items, key) {
  storage.set(key, items)
}

  使用:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
function saveSearch(query) {
  const searches = save(query, SEARCH_KEY, (item) => {
    return item === query
  }, maxLen)
  store.commit('setSearchHistory', searches)
}
 
function deleteSearch(query) {
  const searches = remove(SEARCH_KEY, (item) => {
    return item === query
  })
  store.commit('setSearchHistory', searches)
}

  

posted @   1点  阅读(170)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
历史上的今天:
2018-06-12 vue 兄弟组件的传值
点击右上角即可分享
微信分享提示