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

      

    越努力越幸运
  • 相关阅读:
    sqlserver 表操作 SQL篇
    C#知识点汇总
    DDL
    sqlserver2008简介
    面向对象继承
    IO文件流
    【帅刺猬课堂】Winform中使用WPF的UserControl
    KS Gantt甘特图控件通过递归加载无限层级的数据
    Office 每次打开需要重新配置的问题修复方法
    扩展方法
  • 原文地址:https://www.cnblogs.com/guangzhou11/p/14878712.html
Copyright © 2011-2022 走看看