美文网首页
javascript数组操作汇总

javascript数组操作汇总

作者: 爱酒爱剑爱江湖 | 来源:发表于2017-02-20 16:47 被阅读0次

var colors = new Array('red','blue','black');

//检测是否是数组
if(colors instanceof Array){}
if(Arrays.isArray(colors)){//es5新增}

//查找数组项
indexOf()
lastIndexOf()
//排序
sort(); 可传递function作为排序规则
reverse();

//数组操作
//pop push 数组模拟栈操作(FILO)
var item = colors.pop();//'black'
colors.length;//2
var count = colors.push('black');//3
colors.toString()//'red,blue,black'
//shift unshift 数组模拟队列(FIFO)
var item = colors.shift();//'red'
colors.toString();//'blue,black'
var count = colors.unshift('red');//3
colors.toString()//'red,blue,black'

slice(start,end);截取出新的数组,start不能为空。不会改变原数组
var item = colors.slice(1);//["blue", "black"]
var item = colors.slice(0,1);//["red"]

splice()
删除操作 传递2个参数,返回删除项组成的数组 colors.splice(0,1);//["red"]
插入 colors.splice(1,0,'yellow','white')//"red,yellow,white,blue,black"
//1表示起始位置,0表示要删除的项数 无返回值
替换 colors.splice(1,1,'yellow','white')//"red,yellow,white,black"
//返回被替换掉的项的数组
迭代方法
//每一项都返回true则返回true
colors.every(function(item,index,array){return item=='blue'});
//有一项都返回true则返回true
colors.some(function(item,index,array){return item=='blue'});
// 返回符合条件项的数组
colors.filter(function(item,index,array){return item=='blue'})
// 返回每项结果组成的数组
colors.map(function(item,index,array){return item+='blue'})
//执行操作
colors.forEach(function(item,index,array){})

相关文章

  • javascript数组操作汇总

    var colors = new Array('red','blue','black'); //检测是否是数组if...

  • javascript-数组操作汇总

    http://gold.xitu.io/entry/57e0d0c97db2a24eb1c7a867

  • JavaScript数组汇总

    连接:arr.join("连接符")用连接符把数组里面的元素连接成字符串。arr.join("")能无缝连接。 拼...

  • javascript数组操作

    javascript数组操作 今天针对javascript的数组的一些常见操作进行一些讲解,希望对给为开发者有帮助...

  • 数组操作汇总

    数组 JavaScript的Array可以包含任意数据类型,并通过索引来访问每个元素。 要取得Array的长度,直...

  • JavaScript数组去重

    JavaScript中数组的常用操作之数组去重 方法一 方法二

  • JavaScript中数组操作常用方法

    1.检JavaScript中数组操作常用方法测数组 1)检测对象是否为数组,使用instanceof 操作符 2)...

  • JavaScript数组操作

    定义一个空数组:var arr = [];在数组中添加元素:arr = ['苹果','香蕉','梨','橘子','...

  • Javascript数组操作

    使用JS也算有段时日,然对于数组的使用,总局限于很初级水平,且每每使用总要查下API,或者写个小Demo测试下才算...

  • javaScript 数组操作

    概要 实现一些常用的数组操作函数。 join 用法:[1, 2, 3].join('-') // "1-2-3"实...

网友评论

      本文标题:javascript数组操作汇总

      本文链接:https://www.haomeiwen.com/subject/grxqwttx.html