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

      

    越努力越幸运
  • 相关阅读:
    BFC
    js常用
    uploadify上传文件
    android初探
    springMVC
    java虚拟机
    java编程思想
    eclipse中创建maven项目
    struts2、spring和mybatis整合理解
    struts,spring和mybatis框架整合
  • 原文地址:https://www.cnblogs.com/guangzhou11/p/14878712.html
Copyright © 2011-2022 走看看