let array = ['22222', '333333', '444444'];
1.数组长度
let length = array.length;
console.log(length);
长度:3
2.增加新的元素到数组中 并返回新的数组长度
push(...items: T[]): number;
let array = ['22222', '333333', '444444'];
//添加新元素到数组上
let length = array.push('new');
console.log(length+'-'+array.join('-'));
输出结果:4-22222-333333-444444-new
array = ['22222', '333333', '444444','new']; 执行完push()新数组
3.移除最后一个元素被返回最后一个元素
let array = ['22222', '333333', '444444'];
let pop = array.pop();
输出结果:pop 444444
array=['22222', '333333']
4.组合两个或更多的数组,将新增数组放在数组末尾
let array = ['22222', '333333', '444444'];
array = array.concat(['a','b','c']);
array.join('-')
输出结果:
array = ['22222', '333333', '444444','a','b','c'];
5.通过分隔符将数组连接字符串join(分隔符)
let array = ['22222', '333333', '444444'];
array = array.concat(['a','b','c']);
array.join('-')
输出结果:22222'-333333-444444-a-b-c
6.数组反转
let array = ['a','b','c'];
array = array.reverse();
输出结果:['c','b','a']
7.移除数组第一个元素并返回移除的第一个元素
let array = ['a','b','c'];
let firstElement = array.shift();
输出结果:a
8.从指定位置删除元素
// 数组删除一个或多个 index:从第几个开始 length:删除几个,默认为1,可不传
static arrDelete(arr, index, length = 1) {
let tempArr = arr;
arr.splice(index, length);
return tempArr;
}
9.数组排序
let params = new Map();
let arrayObj=Array.from(params);
//如果是想按key排序,只需要更改一下sort方法为,升序
let paramsStr = '';
arrayObj.sort(function(a,b){return a[0].localeCompare(b[0])});
10.插入元素到第一位置
unshift(...items: T[]): number;
let index = this.state.searchHistory.length -1;
tempArr = Tools.arrDelete(this.state.searchHistory, index);
tempArr.unshift(text);
11.获取某个元素的位置
array.indexOf();//存在返回元素位置,不存在返回-1
12.获取最后出现元素的位置
array.lastIndexOf(元素);
13.循环遍历元素
array.forEach(function(currentValue, index, arr), thisValue)
参数 | 描述 | ||||||||
---|---|---|---|---|---|---|---|---|---|
function(currentValue, index, arr) | 必需。 数组中每个元素需要调用的函数。 函数参数:
| ||||||||
thisValue | 可选。传递给函数的值一般用 "this" 值。 如果这个参数为空, "undefined" 会传递给 "this" 值 |
let result = '';
colors.forEach(function (item, index) {
result =result.concat(item);
});
14.循环遍历元素
array.map(function(currentValue,index,arr), thisValue)
参数 | 描述 | ||||||||
---|---|---|---|---|---|---|---|---|---|
function(currentValue, index,arr) | 必须。函数,数组中的每个元素都会执行这个函数 函数参数:
| ||||||||
thisValue | 可选。对象作为该执行回调时使用,传递给函数,用作 "this" 的值。 如果省略了 thisValue,或者传入 null、undefined,那么回调函数的 this 为全局对象。 |
let buttons = []
{buttons.map((item,i)=>this.CreateBtns(item,i, this.state.buttons.length))}
CreateBtns(item,i,count){
return <CreateButton key={i} btnW={this.btnW} onClick={this.hide.bind(this)} item={item} indexs={i} count={count}/>
}
15.查找符合条件的元素
array.filter(function(currentValue,index,arr), thisValue)
let result = colors.filter(function (item) {
return item === 'green';
});
结果是:['green']满足条件的数组