标签:
获取数组元素索引:
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());
标签:
原文地址:http://www.cnblogs.com/sky-zhao/p/5054208.html