zoukankan      html  css  js  c++  java
  • js 操作数据方法

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

      使用:

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

      

    越努力越幸运
  • 相关阅读:
    find命令进阶(二):对找到的文件执行操作exec
    find命令进阶用法(一)
    find按照文件大小查找
    find命令查找目录
    什么是ppa
    Linux进程管理命令
    [HDOJ4135]Co-prime
    [HDOJ5391]Zball in Tina Town
    [模拟]位运算实现四则运算
    [HDOJ1233]还是畅通工程
  • 原文地址:https://www.cnblogs.com/guangzhou11/p/14878712.html
Copyright © 2011-2022 走看看