Array对象
lenght
concat() 方法用于合并两个或多个数组。此方法不会更改两大有数组,而是返回一个新的数组
let arr1 = ["a","b","c"];
let arr2 = ["d","e","f"]
let arr3 = arr1.concat(arr2)
console.log(arr3);// [ "a", "b", "c", "d", "e", "f" ]
console.log(arr1);// ["a","b","c"]
console.log(arr2);// ["d","e","f"]
join() 方法将数组(或一个类数组对象)的所有元素连接到一个字符串中
let a = ['Wind','Rain','Fire'];
a.join(); // 'Wind,Rain,Fire'
//默认为 ","
a.join(" "); // 空字符串
// "WindRainFire"
a.join("-");
//分隔符 " - "
//结果:'Wind-Rain-Fire'
console.log(a);
// ['Wind','Rain','Fire']
pop() 方法从数组中删除最后一个元素,并返回该元素的值。此方法更改数组的长度。
let a = [1,3,4];
a.length;// 3
var p = a.pop();
console.log(p);// 4
console.log(a.length); // 2
push() 方法将一个或多个元素添加到数组的末尾,并返回数组的新长度。
var numbers = [1,2,3];
numbers.push(4);
console.log(numbers);
// [1,2,3,4]
numbers.push(5,6,7);
console.log(numbers)
//[1,2,3,4,5,6,7]
reverse() 方法将数组中元素的位置颠倒
- 第一个数组元素为最后一个数组元素,最后一个数组元素成为第一个。
shift() 方法从数组中删除第一个元素,并返回该元素的值。此方法更改数组的长度
let a = [1,2,3];
let b = a.shift();
console.log(a);
// [2,3]
console.log(b);
// 1
unshift() 方法将一个或多个元素添加到数组的开头,并返回新数组的长度。
let a = [1,3,5]
a.unshift(8,9);
console.log(a);
// [8,9,1,3,5]
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","drum","surgeon"]
//被删除元素数组:["mandarin"]
//从第 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"]
slice() 方法返回一个从开始到结束(不包括结束)选择的数组的一部分浅拷贝到一个新的数组对象,原数组不会被修改
let a = ['zero','one','two','three'];
let sliced = a.slice(1,3)
console.log(sliced);
// ['one','two']
console.log(a);
// ['zero','one','two','three']
sort()方法在适当的位置对数组的元素进行排序,并返回数组,sort排序不一定是稳定的,默认排序顺序是根据字符串Unicode码点。
- 语法
- arr.sort()
- arr.sort(compareFunction)
- 参数compareFunction 用来指定按某种顺序进行排列的函数
var fruit = ['cherries','apples','bananas'];
fruit.sort();
//['apples','bananas','cherries']
var scores = [1,10,29,2];
scores.sort();
//[1,10,2,20]
//注意10在2之前,因为在Unicode中,10在2之前
var things = ['word', 'Word', '1 Word', '2 Words'];
things.sort();
// ['1 Word', '2 Words', 'Word', 'word']
// 在Unicode中, 数字在大写字母之前,
// 大写字母在小写字母之前.
// 例 --------------------------------
//数字排序
var number = [3,6,2,5,7]
number.sort(function(a,b){
return a-b; //升序
//return b-a; //降序
})
console.log(number);
// [2,3,5,6,7]
map()方法创建一个新数组,其结果是该数组中的第一个元素都调用一个提供的函数后返回的结果
let numbers = [1, 5, 10, 15];
let doubles = numbers.map( x => x ** 2);
console.log(doubles); // [1,25,100,225]
const numbers2 = [2, 4, 8, 10];
let halves = numbers2.map(x => x / 2);
console.log(halves); // [1,2,4,5]
let numbers3 = [1, 4, 9];
let roots = numbers3.map(Math.sqrt);
console.log(roots); // [1, 2, 3]
// numbers : [1, 4, 9]
/**
以下代码将一个包含对象的数组用以创建一个包含新重新格式化对象的新数组
*/
var kvArray = [
{key:1,value: 10},
{key:2,value: 20},
{key:3,value: 30},
{key:4,value: 40}
]
//
// var newArray = kvArray.map(function (obj) {
// var rObj = {};
// rObj[obj.key] = obj.value;
// return rObj;
// })
// 箭头函数
var newArray = kvArray.map((obj) => {
var rObj = {};
rObj[obj.key] = obj.value;
return rObj;
})
console.log(newArray);
forEach() 方法对数组的每个元素执行一次提供的函数,没有返回值和新数组
const arr = ['a','b','c'];
arr.forEach(function(element){
console.log(element);
})
arr.forEach(e => console.log(e))
function logArrayElements(element,index,array){
console.log("a["+index+ "] = " + element)
}
[2,5,,9].forEach(logArrayElements)
// a[0] = 2
// a[1] = 5
// a[3] = 9
[2, 5,"" ,9].forEach(logArrayElements);
// a[0] = 2
// a[1] = 5
// a[2] =
// a[3] = 9
filter() 方法创建一个新数组,其包含通过所提供函数实现的测试的所有元素
function isBigEnough(value){
return value >= 10;
}
var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
conosle.log(filtered); // [12,130,44]
// ES6
const isBigEnough = value => value >= 10;
let [...spread]= [12, 5, 8, 130, 44];
let filtered = spread.filter(isBigEnough);
// filtered is [12, 130, 44]