码迷,mamicode.com
首页 > 编程语言 > 详细

javascript笔记--数组

时间:2015-12-17 16:00:00      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

获取数组元素索引:

indexOf返回第一个找到的索引:可带起始索引

lastIndexOf返回最后一个找到的索引:可带起始索引

var animals = new Array("dog", "cat", "seal" , "elephant", "walrus", "lion","cat")

console.log(animals.indexOf("cat")); //prints 1
console.log(animals.lastIndexOf("cat"));//prints 6

console.log(animals.indexOf("cat", 2)); //prints 6
console.log(animals.lastIndexOf("cat", 4)); //prints 1

 

合并多维数组为一维数组:concat with apply method

var fruitarray = [];
fruitarray[0] = [‘strawberry‘, ‘orange‘];
fruitarray[1] = [‘lime‘,‘peach‘,‘banana‘];
fruitarray[2] = [‘tangerine‘, ‘apricot‘];
fruitarray[3] = [‘raspberry‘, ‘kiwi‘];

var newarray = fruitarray.concat.apply([], fruitarray);
console.log(newarray[5]);

 

删除或替换数组元素:indexOf splice

splice() method takes three parameters; the first parameter is required,as

It‘s index where the splicing is to take place; the second,optional parameter is 

the number of elements to remove; the third parameter ,also optional, is a set of

the replacement elements.

animals.splice(animals.indexOf("walrus"),1);
animals.splice(animals.lastIndexOf("cat"),1, "monkey");
console.log(animals.toString());

javascript笔记--数组

标签:

原文地址:http://www.cnblogs.com/sky-zhao/p/5054208.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!