JS中的一些数组方法简介
时间:2023-04-18 14:37:00
文章目录
- 前言
-
- 一、对象拷贝法
- 二、数组方法
-
- 1、concat()
- 2、copyWithin()
- 3、entry()
- 4、every()
- 5、fill()
- 6、filter()
- 7、find()
- 8、findIndex()
- 9、forEach()
- 10、includes
- 11、indexOf()
- 12、isArray()
- 13、join()
- 14、lastIndexOf()
- 15、map()
- 16、pop()
- 17、reduce()
- 18、some(),toString()
- 总结
前言
JS介绍数组方法
一、对象拷贝法
/*** *复制对象的方法 * **/ var obj = [ {
id: 1, name: "zwt", age: 18}, {
id: 2, name: "bsq", age: 12}, ]; // 相同的引用 var obj2 = obj; console.log(obj2 == obj); //返回true 直接复制只是复制对象的指针,仍然指向同一对象,地址没有变化 ///引用不同 var obj3 = JSON.parse(JSON.stringify(obj)); console.log(obj3 == obj); //false 通过json复制后的地址不同 console.log(obj3);
二、数组方法
1、concat()
/*** * 01、concat() * concat() 该方法用于连接两个或多个数组。现有数组不会改变, * 回到一个新的数组,包括连接数组的值。 * **/ let s1 = ["aaa", "bbb", "ccc", "dd"]; let s2 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
let news1 = s1.concat(s2);
console.log(news1);
2、copyWithin()
/*** * 02、copyWithin() 将数组元素复制到数组中的另一个位置,覆盖现有值。 此方法永远不会向数组添加更多项。注意:此方法会覆盖原始数组。 * **/
let s3 = ["banana", "11", "apple", 11, "11", "apple"];
// s3.copyWithin(被覆盖的下标,要复制的对象下标)
console.log(s3.copyWithin(2, 1)); //即s3中的apple被替换成11
3、entry()
/*** *03、entry() entry() 方法返回一个带有键/值对的数组迭代器对象。 * **/
let s4 = s3.entries();
console.log(s4);
4、every()
/*** *04、every() every() 方法检查数组中的所有元素是否都通过了测试(作为函数提供)。 即相当于对数组中的多个值送进去函数遍历,返回一个boolean值 * **/
let s5 = [23, 28, 65, 62];
console.log(s5);
function check(age) {
return age.id >= 30;
}
let s6 = s5.every(check);
console.log(s6);
5、fill()
/*** *05、fill(x),将原数组内容全部替换为x * **/
let s7 = ["a", "b", "c", "d", "e"];
console.log(s7.fill("hh"));
console.log(s7);
6、filter()
/*** * 06、filter() filter() 方法创建一个数组, 其中填充了所有通过测试的数组元素(作为函数提供)。 * **/
let s8 = [23, 28, 65, 62];
function f1(age) {
return age < 30;
}
console.log("filter方法:", s8.filter(f1));
7、find()
/*** * 07、find() find() 方法返回数组中通过测试的第一个元素的值(作为函数提供)。 * **/
function f2(age) {
return age > 25;
}
console.log(s8.find(f2));
8、findIndex()
/*** * 08、findIndex() findIndex() 方法返回数组中通过测试的第一个元素的索引(作为函数提供)。 * **/
console.log(s8.findIndex(f2));
9、forEach()
/*** * 09、forEach() :会改变原数组 forEach() 方法按顺序为数组中的每个元素调用一次函数 * **/
let s9 = [23, 28, 65, 62];
s9.forEach(f3);
function f3(zhi, xiabiao, shuzu) {
shuzu[xiabiao] = zhi * 10;
}
console.log(s9);
10、includes
/** *10、contains():.includes(是否存在的值,指定下标(可选)) contains() 方法确定数组是否包含指定的元素 **/
let s10 = [
23,
28,
65,
62,
{
a: 1, b: 2, c: 3, d: 4 },
{
a: 1, b: 2, c: 3, d: 4 },
];
let news10 = s10.includes(28);
console.log(news10);
11、indexOf()
/** * 11、indexOf() :返回指定项的指定下标.不存在则是-1 indexOf() 方法在数组中搜索指定项,并返回其位置。 **/
console.log(s10.indexOf(62));
12、isArray()
/** * 12、isArray() isArray() 方法确定对象是否是数组。 如果对象是数组,Thinction 返回 true,否则返回 false。 **/
console.log(Array.isArray(s10));
13、join()
/** * 13、join() :.join(',')则将数组转化为字符串,以“ , ” 隔开 将数组的元素转换为字符串。join() 方法将数组作为字符串返回。 **/
console.log(s10.join(" ^_^ "));
console.log(s10);
14、lastIndexOf()
/** * 14、lastIndexOf() lastIndexOf() 方法在数组中(从后往前,与indexOf相反)搜索指定项,并返回其位置。 **/
console.log(s8.lastIndexOf(62));
15、map()
/** * 15、map():不改变原数组 map() 方法使用为每个数组元素调用函数的结果创建一个新数组。 **/
let s11 = [3, 6, 97, 99];
let news11 = s11.map(Math.sqrt);
console.log(news11);
console.log(s11);
16、pop()
/** * 16、pop() pop() 方法删除数组的最后一个元素,并返回被删除的元素。 17、push() push() 方法将新项添加到数组的末尾,并返回新长度。 :都会改变原数组 **/
console.log(s11.pop());
console.log(s11);
console.log(s11.push("12", "1"));
console.log(s11);
17、reduce()
具体用法可以参考:reduce用法
/** * 18、reduce() reduce() 方法为数组的每个值(从左到右)执行提供的函数, 并将数组缩减为单个值。 **/
let s12 = [3, 6, 97, 99];
function f3(a) {
return a > 4;
}
let news12 = s12.reduce(f3, 0);
console.log(news12);
18、some(),toString()
/** * 21、some() some() 方法检查数组中的任何元素是否通过测试(作为函数提供)。 它为数组中存在的每个元素执行一次函数。 26、toString() toString() 方法返回一个包含所有数组值的字符串,以逗号分隔。 **/
console.log(s12.toString());
总结
介绍JS的一些基础用法。