当前位置:   article > 正文

JS中数组的操作方法_function add(n,limit){ let arr=new array(limit).fi

function add(n,limit){ let arr=new array(limit).fill(0); arr[0]=1;//初始满一


常见的一些数组操作

1、push () 尾部添加元素

语法: array.push(item1, item2, …, itemX)
方法:可以将一个或者更多的参数添加在数组的尾部;返回添加后的数组的长度,原数组发生改变

var arr=[1,2,3,4];
var a=arr.push(9,8,7);
console.log(a,arr);//1,2,3,4,9,8,7;
  • 1
  • 2
  • 3

2、unshift () 头部添加元素

语法: array.unshift(item1,item2, …, itemX)
方法:可以将一个或者更多的参数添加在数组的头部;返回添加后的数组的长度,原数组发生改变

var arr=[1,2,3,4];
var a=arr.unshift(9,8,7);
console.log(a,arr);//9,8,7,1,2,3,4;
  • 1
  • 2
  • 3

3、pop () 删除尾部元素

语法:array.pop()
方法:从数组尾部删除一个元素,返回这个被删除的元素,原数组发生改变

var arr=[1,2,3,4];
var a=arr.pop();
console.log(a,arr)//4;1,2,3,
  • 1
  • 2
  • 3

4、shift () 删除头部元素

语法:array.shift()
方法:从数组头部删除一个元素,返回这个被删除的元素,原数组发生改变

var arr = [1,2,3,4];
var a = arr.shift();
console.log(a,arr)//1;2,3,4,
  • 1
  • 2
  • 3

5、slice () 截取数组(原数组不变)

语法:array.slice(start, end) 最多接受两个参数
方法:不传参数,会返回原数组。传一个参数,从该参数表示的索引开始截取,直至数组结束,返回这个截取数组原数组不变。传两个参数,从第一个参数对应的索引开始截取,到第二个参数对应的索引结束,但包括第二个参数对应的索引上的值,返回这个截取数组原数组不改变

var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = fruits.slice(1,3);
console.log(citrus )//Orange,Lemon
  • 1
  • 2
  • 3

6、splice () 截取数组(原数组改变)

语法:array.splice(index,howmany,item1,…,itemX)
方法:没有参数,返回空数组,原数组不变。一个参数,从该参数表示的索引位开始截取,直至数组结束,返回截取的数组,原数组改变。两个参数,第一个参数表示开始截取的索引位,第二个参数表示截取的长度,返回截取的 数组,原数组改变;三个或者更多参数,第三个及以后的参数表示要从截取位插入的值。

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2,2);
console.log(fruits )//Banana,Orange
  • 1
  • 2
  • 3

7、reverse () 颠倒数组顺序

语法:array.reverse()
方法:用于颠倒数组中元素的顺序

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.reverse();
console.log(fruits)//Mango,Apple,Orange,Banana
  • 1
  • 2
  • 3

8、sort () 数组排序

语法:array.sort(sortfunction)
方法:用于对数组的元素进行排序。

var Array = [1,2,3,4,5];
var fruits = Array.sort(function(a,b){
   // return a - b; //从小到大
    return b-a; // [5,4,3,2,1]
})
  • 1
  • 2
  • 3
  • 4
  • 5

9、join () 数组所有元素转化为字符串

语法:array.join(separator)
方法:数组中的所有元素转换一个字符串,元素是通过指定的分隔符进行分隔的。

var arr = [1,2,3,4]
var ass = arr.join()
console.log(ass)//1,2,3,4
  • 1
  • 2
  • 3

10、concat () 拼接数组或字符串

语法:string.concat(string1, string2, …, stringX)
方法:字符串的方法,但数组也可以用,接受一个或多个参数,将参数中的值放到操作的数组后边,返回拼接的数组,原数组不变。如果参数是一个数组,则先把值提取出来再操作。

var arr = [1,2,3,4];
arr.concat([5,6,7])//[1,2,3,4,5,6,7]
  • 1
  • 2

ES5新增的一些数组方法

1、indexOf () 查询数组元素首次出现的位置

语法:string.indexOf(searchvalue,start)
方法:字符串的方法,数组也可适用,此方法可返回某个指定的字符串值在字符串中首次出现的位置。若一个参数,返回这个参数在数组里面的索引值,若参数不在操作的数组中,则返回 -1。

var arr = [1,2,3,4];
arr.indexOf(1) // 0
arr.indexOf(5) // -1 
  • 1
  • 2
  • 3

2、forEach () 遍历数组

语法:array.forEach(function(currentValue, index, arr), thisValue)
方法:数组遍历,且只能够遍历数组,不接受返回值或返回值为 undefined。
如果数组只有长度没有初始化是不会迭代元素的

var arr = [1,2,3,4,5];
arr.forEach((item,index,arr)=>{
   //item 为当前数组元素
   // index 为当前索引
   // arr 为数组名字
})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

3、map () 遍历数组

语法:array.map(function(currentValue,index,arr), thisValue)
方法:数组的遍历,用来接收一个返回值,创建一个新数组,不改变原数组

var arr = [1,2,3,4,5,6];
arr.map(function(item,index,arr){
    return item
}) // [1, 2, 3, 4, 5, 6]
  • 1
  • 2
  • 3
  • 4

4、filter () 过滤数组

语法:array.filter(function(currentValue,index,arr), thisValue)
方法:过滤出一些符合条件的元素,返回一个新数组不改变原数组

var ages = [32, 33, 16, 40];
function checkAdult(age) {
    return age >= 18; //返回数组 ages 中所有元素都大于 18 的元素:
}
function myFunction() {
    return ages.filter(checkAdult);
}
myFunction(ages) // [32, 33, 40]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

5、some () 检测数组中是否有符合条件的元素

语法:array.some(function(currentValue,index,arr),thisValue)
方法:检测数组中是否含有某一个值,返回一个布尔值,如果数组中有任意一个元素满足给定的条件,结果就为 true,否则为false。

var ages = [3, 10, 18, 20];
function checkAdult(age) {
    return age >= 18;
}
function myFunction() {
    return ages.some(checkAdult);
}
myFunction(ages) //输出结果为:true
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

6、every () 检测数组中所有元素都符合条件

语法:array.every(function(currentValue,index,arr), thisValue)
方法:方法用于检测数组所有元素是否都符合指定条件(通过函数提供),满足条件返回true,否则返回false。

var ages = [32, 33, 16, 40];
function checkAdult(age) {
    return age >= 18;
    //检测数组 ages 的所有元素是否都大于等于 18 
}
function myFunction() {
    return ages.every(checkAdult);
}
myFunction(ages) //输出结果为:false
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

7、reduce () 对数组中所有元素调用指定回调函数

语法:array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
方法:对数组中的所有元素调用指定的回调函数,该回调函数的返回值为累计结果。并且把返回值在下一次回调函数时作为参数提供。

var ages = [65, 44, 12, 4];
function getAge(total, num) {
    return total + num;
    //计算数组相加的总和
}
function myFunction(item) {
    return ages.reduce(getAge);
}
myFunction(ages) //输出结果为:125
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

ES6新增的数组方法

1、Array.from( ) 数组对象转化为数组

语法:Array.from(arrayLike[, mapFn[, thisArg]])
方法:将类数组对象或可迭代对象转化为数组,比如arguments,js选择器找到dom集合和对象模拟的数组。

// 参数为数组,返回与原数组一样的数组
console.log(Array.from([1, 2])); // [1, 2] 
// 参数含空位
console.log(Array.from([1, , 3])); // [1, undefined, 3]
  • 1
  • 2
  • 3
  • 4

2、Array.of( ) 返回数组

方法:数组创建,将参数中所有值作为元素形成数组,如果参数为空,则返回一个空数组。

console.log(Array.of(1, 2, 3, 4)); // [1, 2, 3, 4] 
// 参数值可为不同类型
console.log(Array.of(1, '2', true)); // [1, '2', true] 
// 参数为空时返回空数组
console.log(Array.of()); // []
  • 1
  • 2
  • 3
  • 4
  • 5

3、find( ) 查找数组中符合条件的元素

方法:查找数组中符合条件的元素,若有多个符合条件的元素,则返回第一个元素

let arr = Array.of(1, 2, 3, 4);
console.log(arr.find(item => item > 2)); // 3 
// 数组空位处理为 undefined
console.log([, 1].find(n => true)); // undefined
  • 1
  • 2
  • 3
  • 4

4、findIndex( ) 查找数组中符合条件的元素索引

方法:查找数组中符合条件的元素索引,若有多个符合条件的元素,则返回第一个元素索引

let arr = Array.of(1, 2, 1, 3);
// 参数1:回调函数
// 参数2(可选):指定回调函数中的 this 值
console.log(arr.findIndex(item => item == 1)); // 0
// 数组空位处理为 undefined
console.log([, 1].findIndex(n => true)); //0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

5、includes () 检测数组中是否包含值

方法:检测数组中是否包含一个值。
注意:与 Set 和 Map 的 has 方法区分;Set 的 has 方法用于查找;Map 的 has 方法用于查找键名

// 参数1:包含的指定值
[1, 2, 3].includes(1);    // true
// 参数2:可选,搜索的起始索引,默认为0
[1, 2, 3].includes(1, 2); // false
// NaN 的包含判断
[1, NaN, 3].includes(NaN); // true
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

6、fill () 替换数组中某个元素

方法:将一定范围索引的数组元素内容填充为单个指定的值。如果传入的是对象则每个元素都指向同一个元素

let arr = Array.of(1, 2, 3, 4);
// 参数1:用来填充的值
// 参数2:被填充的起始索引
// 参数3(可选):被填充的结束索引,默认为数组末尾
console.log(arr.fill(0,1,2)); // [1, 0, 3, 4]
  • 1
  • 2
  • 3
  • 4
  • 5

7、entries () 遍历键值对

方法:遍历键值对。

for(let [key, value] of ['a', 'b'].entries()){
    console.log(key, value);
}
// 0 "a"
// 1 "b" 
// 不使用 for... of 循环
let entries = ['a', 'b'].entries();
console.log(entries.next().value); // [0, "a"]
console.log(entries.next().value); // [1, "b"]
// 数组含空位
console.log([...[,'a'].entries()]); // [[0, undefined], [1, "a"]]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

8、keys () 遍历键名

方法:遍历键名

for(let key of ['a', 'b'].keys()){
    console.log(key);
}
// 0
// 1 
// 数组含空位
console.log([...[,'a'].keys()]); // [0, 1]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

9、values () 遍历键值

方法:遍历键值

for(let value of ['a', 'b'].values()){
    console.log(value);
}
// "a"
// "b"
// 数组含空位
console.log([...[,'a'].values()]); // [undefined, "a"]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

10、flat () 嵌套数组转化为一维数组

方法:嵌套数组转化一维数组

console.log([1 ,[2, 3]].flat()); // [1, 2, 3] 
// 指定转换的嵌套层数
console.log([1, [2, [3, [4, 5]]]].flat(2)); // [1, 2, 3, [4, 5]] 
// 不管嵌套多少层
console.log([1, [2, [3, [4, 5]]]].flat(Infinity)); // [1, 2, 3, 4, 5]
// 自动跳过空位
console.log([1, [2, , 3]].flat());<p> // [1, 2, 3]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

11、at() 接受整数

方法:接受一个整数作为参数,返回对应位置的成员,并支持负索引

console.log([1 ,2, 3].at(-1)); // 3 
console.log([1 ,2, 3].at(1)); // 2 
// 超出
console.log([1 ,2, 3].at(100)); //undefined
  • 1
  • 2
  • 3
  • 4

12、扩展运算符

…:扩展运算符
let arr = [1, 2],
    arr1 = [...arr];
console.log(arr1); // [1, 2]
// 数组含空位
let arr2 = [1, , 3],
    arr3 = [...arr2];
console.log(arr3); [1, undefined, 3]
//合并数组
console.log([...[1, 2],...[3, 4]]); // [1, 2, 3, 4]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/477040
推荐阅读
相关标签
  

闽ICP备14008679号