JS-数组常用方法1

166 阅读3分钟

slice 截取 数组/字符串 (不改变原数组)

  • 截取数组,返回新数组;
  • 截取字符串,返回新字符串;
  • 将类似数组的对象转为真数组
  • slice:截取数组字符串 不改变原数组 原字符 [start,end)
    • [start,end) 正着从0开始 倒着从-1开始
    • array.slice(start, end) start 为负数则倒着数 end不写则截取到最后一个元素
// 截取数组 以新数组返回选定的元素 
let fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];

let cutfruits = fruits.slice(1, 3);
console.log(cutfruits); // ['Orange', 'Lemon']

let cutfruits2 = fruits.slice(-2, -1);
console.log(cutfruits2); // ['Apple']

let cutfruits3 = fruits.slice(-2);
console.log(cutfruits3); //['Apple', 'Mango']
// 截取字符串 以新字符串返回选定的部分
let str = 'xzlpllz';

let cutstr = str.slice(2, 3); // 即[2,3)
console.log(cutstr); //l

let cutstr2 = str.slice(-5, -1); // 即[-5,-1)
console.log(cutstr2); //lpll

splice 添加删除再添加 (会改变原数组)

  • 添加数组元素;删除数组元素;
  • 删除再添加数组元素;
  • 返回的都是被操纵的元素组成的数组
  • splice:从数组中添加或删除或删除再添加元素 会改变原数组
    • [start,howmany]
    • array.splice(start,howmany,item1,.....,itemX)
    • 定义:起始位 多少个元素 要添加到数组的新元素
    • 返回值:
      • 如果没有删除 返回空数组
      • 如果删除了 则返回的是含有被删除的元素的数组
// 添加元素
let people1 = ["小肖", "小杨", "小悦"];
let peopleadd = people1.splice(1, 0, "apple", "banaer");
console.log(peopleadd); // [] 如果没有删除 返回空数组
console.log(people1); // ['小肖','apple','banaer','小杨','小悦']
// 删除元素
let people2 = ["apple", "banner", "orange"];
let peopledel = people2.splice(1, 2);
console.log(peopledel); // ['banner','orange'] 返回删除的元素数组
console.log(people2); // ['apple'] 
// 删除再添加元素
let people3 = [1, 2, 3, 4, 5, 6];
let peopledeladd = people3.splice(1, 2, "apple", "banaer");
console.log(peopledeladd); // [2, 3] 如果删除了 则返回删除的元素数组
console.log(people3); // [1,'apple','banaer',4,5,6]

添加

let array = [1, 2, 3, 4];
  • push:向尾部 改变原数组 返回新数组长度
let arrayPush = array.push('push');
console.log(array); // [ 1, 2, 3, 4, 'push' ]
console.log(arrayPush); //5
  • unshift:向头部 改变原数组 返回新数组长度
let arrayUnshift = array.unshift('unshift');
console.log(array); // [ 'unshift', 1, 2, 3, 4, 'push' ]
console.log(arrayUnshift); // 6

删除

let brray = [4,3,2,1];
  • pop:向尾部删除 改变原数组 返回删除元素
let brrayPop = brray.pop();
console.log(brray); // [ 4, 3, 2 ]
console.log(brrayPop); // 1
  • shift:向头部删除 改变原数组 返回删除元素
let brrayShift = brray.shift();
console.log(brray); // [ 3, 2 ]
console.log(brrayShift); // 4

合并

  • concat:合并数组 不改变原数组 返回新数组
    • array1.concat(array2,array3,...,arrayX)
    • 该参数可是具体的值 也可是数组对象 可以是任意多个
let arr1 = [1, 2];
let arr2 = ['小肖', '小杨'];
let arr3 = [undefined, null, 0];
let arr = arr1.concat(arr2, arr3)
console.log(arr); // [ 1, 2, '小肖', '小杨', undefined, null, 0 ]
console.log(arr1); // [ 1, 2 ]

分割数组并转字符串

  • toString:array.toString() 数组转字符串 逗号分隔
let arrr = ['a', 'b', 'c', 'd'];
console.log(arrr.toString()); //a,b,c,d
  • join:array.join(separator) 数组转字符串 指定字符分隔
let arr = ['小肖', '小杨', 'apple', 1];

let newarr1 = arr.join();
console.log(arr); //[ '小肖', '小杨', 'apple', 1 ]
console.log(newarr1); //小肖,小杨,apple,1

let newarr2 = arr.join('|');
console.log(newarr2); //小肖|小杨|apple|1

其它 计算 排序 翻转 循环

  • 计算
    • array.reduce(function(total, item, index, arr), initialValue)
    • 初始值, 或者计算结束后的返回值必选 必选 可选 可选
let addArr = [1,2,3,4];
let addArrResult = addArr.reduce(function(total, item, index, arr){
    console.log(total);//1 3 6 
    return total + item
});
console.log(addArrResult,'s'); //10
  • 排序
    • 会改变原数组
    • 按字母排序 直接调用 按数字排序 必须通过一个函数作为参数调用 指定升序还是降序
    • array.sort(sortfunction)
let number  = [1,2,0,5,-2];
console.log(number.sort(function(a,b){return a-b})); // [ -2, 0, 1, 2, 5 ]
console.log(number.sort(function(a,b){return b-a})); // [ 5, 2, 1, 0, -2 ]
console.log(number); // [ 5, 2, 1, 0, -2 ]
let word =  ["Banana", "Orange", "Apple", "Mango"];
console.log(word.sort()); // [ 'Apple', 'Banana', 'Mango', 'Orange' ]
console.log(word); // [ 'Apple', 'Banana', 'Mango', 'Orange' ]
  • 翻转
    • array.reverse()
let fruits = ["Banana", "Orange", "Apple", "Mango"];
console.log(fruits.reverse()); 
// [ 'Mango', 'Apple', 'Orange', 'Banana' ]
  • 循环
    • 数组每个元素都执行一次回调函数
    • array.forEach(function(currentValue, index, arr), thisValue)
let people = [{name: '小肖',age: 30}, {name: '小杨',age: 23}];
people.forEach(item => console.log(item));
// { name: '小肖', age: 30 } { name: '小杨', age: 23 }
people.forEach(function (item, index, arr) {
    console.log(item);
    console.log(index);
    console.log(arr);
})
/*
    0
    [ { name: '小肖', age: 30 }, { name: '小杨', age: 23 } ]
    { name: '小杨', age: 23 }
    1
    [ { name: '小肖', age: 30 }, { name: '小杨', age: 23 } ]
*/