zoukankan      html  css  js  c++  java
  • 数组的一些操作

    1、var ArrayX = [0];
    //判断元素是否在数组里
    Array.prototype.inArray = function(e)
    {
    for(i=0;i<this.length && this[i]!=e;i++);
    return !(i==this.length);
    }

    使用方法:ArrayName.inArray(val)
    2、//删除数据元素
    Array.prototype.remove=function(dx)
    {
      if(isNaN(dx)||dx>this.length){return false;}
      for(var i=0,n=0;i<this.length;i++)
      {
        if(this[i]!=this[dx])
        {
          this[n++]=this[i]
        }
      }
      this.length-=1
     }

    使用方法:ArrayName.remove(xiabiao);
    3、//获取元素在数组中的位置
    function searchKeys(needle, haystack){
    var result = [];
    for (i in haystack)
    {
    if (haystack[i] == needle)
    {
    result.push(i);
    }
    }
    return result;
    }

    使用方法:searchKeys(val,ArrayName);

  • 相关阅读:
    React-使用combineReducers完成对数据对拆分管理
    Linux
    Linux
    linux
    linux
    Linux
    Linux
    Linux
    Linux
    Linux 系统基础优化和常用命令
  • 原文地址:https://www.cnblogs.com/tengzhouboy/p/3152560.html
Copyright © 2011-2022 走看看