数组相关操作

2020-06-07  本文已影响0人  RadioWaves

join()

var arr = [0, 1, 2, 3];
arr.join() // "0,1,2,3,"

unshift()

var arr = [0,1,2];
var count = arr.unshift(3, 4, 5)   // 6
console.log(arr) // 3, 4, 5, 0, 1, 2 

push()

var arr = [0,1,2];
var count = arr.push(3, 4, 5)   // 6
console.log(arr) // 0, 1, 2, 3, 4, 5  

shift()

var arr = [0, 1, 2];
var item = arr.shift(); // 0
console.log(arr); // [1, 2]

pop()

var arr = [0, 1, 2];
var item = arr.pop(); // 2
console.log(arr); // [0, 1]

reverse()

var arr = [0, 1, 2];
arr.reverse; // [2, 1, 0];

concat()

var arr = [0, 1, 2];
var item = arr.concat(4);
console.log(arr) // [0, 1, 2];
console.log(item) // [0, 1, 2, 4];

indexof()

var arr = [0, 1, 2, 3, 4, 5];
var item1 = arr.indexOf(7);
console.log(item1) // -1
var item2 = arr.indexOf(5);
console.log(item2) // 6

lastIndexOf()

var arr = [0, 1, 2, 3, 4, 5];
var item1 = arr.indexOf(7);
console.log(item1) // -1
var item2 = arr.indexOf(5);
console.log(item2) // 6
上一篇 下一篇

猜你喜欢

热点阅读