Array

let arr=['  xixi ', 'll ll','sdsds '];

数组长度 length

arr.length;  // 3

遍历数组 forEach

arr.forEach(function (item, index, array) {
    console.log(item, index);
});  
/*  
  xixi  0
ll ll 1
sdsds  2
*/

添加元素到数组末尾 push

arr.push('biu');

删除数组末尾的元素 pop

arr.pop();

删除数组最前面的元素 shift

arr.shift();

添加元素到数组头部 unshift

arr.unshift('hhh1');
arr.forEach(function (item, index, array) {
    console.log(item, index);
});
/*
hhh1 0
ll ll 1
sdsds  2
*/

找出某个元素在数组中的索引,否则返回-1 indexOf

arr.indexOf('sdsds '); // 2
arr.push('hhh2','3','4');
arr.forEach(function (item, index, array) {
    console.log(item, index);
});
/*
hhh1 0
ll ll 1
hhh2 2
3 3
4 4
*/

返回某个元素在数组中的索引,从指定位置开始向后查找,否则返回-1,indexOf

arr.indexOf(searchElement, fromIndex);

返回指定元素在数组中最后一个索引,从后往前查找,不存在返回-1, lastIndexOf

arr.lastIndexOf(searchElement[, fromIndex = arr.length - 1]);

通过索引删除某个元素 splice(开始,数量,添加进的元素)

let inx=arr.indexOf('3');
arr.splice(inx,1); // ["3"]
arr.forEach(function (item, index, array) {
console.log(item, index);
});
/*
hhh1 0
ll ll 1
hhh2 2
4 3
*/

从一个索引位置删除多个元素 splice

arr.push('7','290w','0212','2ss','22cdsd');
console.log(arr); // ["hhh1", "ll ll", "hhh2", "4", "7", "290w", "0212", "2ss", "22cdsd"]
let ind=3; //索引开始位置
let n=3; //总共删除几个元素
let removeitems=arr.splice(ind,n);
console.log(arr); // ["hhh1", "ll ll", "hhh2", "0212", "2ss", "22cdsd"]
console.log(removeitems); //["4", "7", "290w"]

删除现有元素和添加新元素来改变数组splice

var myFish = ["angel", "clown", "mandarin", "surgeon"];

//从第 2 位开始删除 0 个元素,插入 "drum"
var removed = myFish.splice(2, 0, "drum");
//运算后的 myFish:["angel", "clown", "drum", "mandarin", "surgeon"]
//被删除元素数组:[],没有元素被删除

//从第 3 位开始删除 1 个元素
removed = myFish.splice(3, 1);
//运算后的myFish:["angel", "clown", "mandarin",]
//被删除元素数组:["surgeon"]

//从第 2 位开始删除 1 个元素,然后插入 "trumpet"
removed = myFish.splice(2, 1, "trumpet");
//运算后的myFish: ["angel", "clown", "trumpet", "surgeon"]
//被删除元素数组:["drum"]

//从第 0 位开始删除 2 个元素,然后插入 "parrot", "anemone" 和 "blue"
removed = myFish.splice(0, 2, "parrot", "anemone", "blue");
//运算后的myFish:["parrot", "anemone", "blue", "trumpet", "surgeon"]
//被删除元素的数组:["angel", "clown"]

//从第 3 位开始删除 2 个元素
removed = myFish.splice(3, Number.MAX_VALUE);
//运算后的myFish: ["parrot", "anemone", "blue"]
//被删除元素的数组:["trumpet", "surgeon"]

//从第1位开始删除其后所有即[1,end]的元素
removed = myFish.splice(1);
//运算后的myFish: ["parrot"]
//被删除元素的数组:["anemone","blue"]

复制一个数组 slice

let arr2=arr.slice(); 
arr2 //["hhh1", "ll ll", "hhh2", "0212", "2ss", "22cdsd"]

将数组中的一部分拷贝到一个新的数组,原数组不变,slice(begin,end)不包括结束

let arr2=["hhh1", "ll ll", "hhh2", "0212", "2ss", "22cdsd"];
let arr3=arr2.slice(1,4);
arr3  // ["ll ll", "hhh2", "0212"]
arr3=arr2.slice(1); // ["ll ll", "hhh2", "0212", "2ss", "22cdsd"]

确定传递的值是否是一个数组 isArray

Array.isArray([1,2,3,4,3]); //true
Array.isArray({foo: 123});// false
Array.isArray("foobar");// false
Array.isArray(undefined);// false

合并两个或多个数组 concat

let num1=[1,2,3];
let num2=[4,5,6];
let num3=[7,8,9];
let num4=num1.concat(num2,num3);
num4 // [1, 2, 3, 4, 5, 6, 7, 8, 9]
num1 // [1, 2, 3]

返回一个新的Array迭代器对象,该对象包含数组中每个索引的键/值对 entries

let arr3=['p','11','qweqwe','pp','tt'];
let iterator = arr3.entries();
iterator //Array Iterator
for(let e of iterator){console.log(e);}
/*
[0, "p"]
[1, "11"]
[2, "qweqwe"]
[3, "pp"]
[4, "tt"]
*/
console.log(iterator.next().value); //undefined

let arr=['1','2',3];
let it=arr.entries();
console.log(it.next().value); //[0, "1"]
console.log(it.next().value);// [1, "2"]
console.log(it.next().value);// [2, 3]
console.log(it.next().value); //undefined

测试数组所有元素是否都通过了指定函数的测试 every

let num=[1,2,3,4,5,6,7,9,10,11];
num.every((item,index,array) => item>0); //true
num.every((item,index,array) => item>10); //false

用一个固定值填充数组 fill(value,start,end)

[1, 2, 3].fill(4)            // [4, 4, 4]
[1, 2, 3].fill(4, 1)         // [1, 4, 4]
[1, 2, 3].fill(4, 1, 2)      // [1, 4, 3]
[1, 2, 3].fill(4, 1, 1)      // [1, 2, 3]
[1, 2, 3].fill(4, -3, -2)    // [4, 2, 3]
[1, 2, 3].fill(4, NaN, NaN)  // [1, 2, 3]
Array(3).fill(4);            // [4, 4, 4]
[].fill.call({length: 3}, 4) // {0: 4, 1: 4, 2: 4, length: 3}

返回一个新数组,其中的元素通过所提供函数的测试 filter

let filter=[2,3,4,5,10,13,23].filter((item,index,array) => item>5);
filter //[10, 13, 23]

返回满足测试函数的第一个元素的值,否则返回undefined find

[2,3,10,24,232,23,1].find((item,index) => item>20); //24

 

返回满足测试函数的第一个元素的索引,否则返回-1 findIndex

[2,3,10,24,232,23,1].findIndex((item,index) => item>20); //3

返回一个数组是否包含一个指定的值 includes

[1,2,3].includes(3); //true
[1,2,3].includes(4); //false

从某个索引开始查询数组是否包含一个指定的值 includes

[1,2,3].includes(3,2); //true
[1,2,3].includes(3,3); //false
[1,2,3].includes(3,-100); //true
[1,2,3].includes(3,-10); //true
[1,2,3].includes(3,-2); //true

将数组中的所有元素连接到一个字符串中(不会改变原数组) join

let arr=['hello','baby','hi','girl'];
arr.join(); //默认用‘,’连接   "hello,baby,hi,girl"
arr.join(''); //"hellobabyhigirl"
arr.join('-'); //"hello-baby-hi-girl"

返回一个新的array迭代器,包含数组中每个索引的键 keys

let arr1=['qq','pp','ww'];
let ite=arr1.keys();
for(let e of ite){console.log(e);}
/*
0
1
2
*/
let arr2=[1,2,3,4,5,6,7];
let i=arr2.keys();
console.log(i.next()); //{value: 0, done: false}
console.log(i.next().value); //1
console.log(i.next().value); //2

返回一个新的array迭代器,包含数组中每个索引的值 values

let arr = ['w', 'y', 'k', 'o', 'p'];
let eArr = arr.values();
console.log(eArr.next().value); // w
console.log(eArr.next().value); // y
console.log(eArr.next().value); // k
console.log(eArr.next().value); // o
console.log(eArr.next().value); // p

创建一个新数组,每个元素为调用函数后的结果 map

let num=[1,2,3,4,2,1,3];
let doublenum=num.map(x => x*2);
doublenum //[2, 4, 6, 8, 4, 2, 6]

对累加器和数组中的每个元素(从左到右)应用一个函数,将其减少为一个值 reduce

let item=[1,2,3].reduce((sum,value) => sum+value, 0); // 6
item=[[1,2],[3,4],[5,6]].reduce((a,b) => a.concat(b),[]); //[1, 2, 3, 4, 5, 6]

接受一个函数作为累加器(accumulator)和数组的每个值(从右到左)将其减少为单个值 reduceRight

let flattened = [
    [0, 1], 
    [2, 3], 
    [4, 5]
].reduceRight((a, b) => {
    return a.concat(b);
}, []);

// flattened is [4, 5, 2, 3, 0, 1]

颠倒数组中的元素 reverse

let num=[1,2,3,4,5];
num.reverse(); //[5, 4, 3, 2, 1]
num //[5, 4, 3, 2, 1]

测试数组中的某些元素是否通过提供函数的测试 some

[1,2,3,4].some((item) => item>0); // true
[1,2,3,4].some((item) => item>10); // false
[1,2,3,4].some((item) => item>3); // true

排序 sort

let num=[3,4,2,4,20,1,9];
num.sort();//  [1, 2, 20, 3, 4, 4, 9]
num.sort((a,b) => a-b); //[1, 2, 3, 4, 4, 9, 20]
num.sort((a,b) => b-a); //[20, 9, 4, 4, 3, 2, 1]

返回一个字符串表示数组中的元素。数组中的元素将使用各自的 toLocaleString 方法转成字符串,这些字符串将使用一个特定语言环境的字符串(例如一个逗号 ",")隔开。toLocaleString

let arr=['hello','welcome',123,'hh'];
arr.toLocaleString(); // "hello,welcome,123,hh"

返回一个字符串toString

let arr=['hello','welcome',123,'hh'];
arr.toString(); //"hello,welcome,123,hh"

 

posted @ 2018-01-08 16:20  biubiu小希希  阅读(269)  评论(0编辑  收藏  举报