1.for....in....(对象和数组都能使用)
let a=['a','b','c']
let b ={a:2,b:3,c:4}
for(let i in a) {
console.log(i)//打印数组索引
console.log(a[i])//循环数组----打印数组对应值
}
for(let i in b) {
console.log(i)//打印对象的属性
console.log(b[i])//循环对象----打印对象属性值
}
2. for...of...
let a=['a','b','c']
for(let i of a) {
console.log(i)//a,b,c----i代表数组中的值
}
3.filter()方法--创建一个新数组,过滤取出满足条件的值
const a=[1,2,3,2,5,4,6,8]
const b= a.filter(e=>e>2)//过滤出大于2的值
console.log(b)
4.map()方法--创建一个新数组,每个元素调用一次提供的方法并返回结果
const numbers = [2, 3, 4, 5];
const dollars = numbers.map( number => '$' + number);
// ['$2', '$3', '$4', '$5']
4.reduce()方法---该方法对于计算总数非常管用。返回值可以是任何类型
const numbers = [5, 10, 15];
const total = numbers.reduce( (accumulator, currentValue) => accumulator + currentValue);
// 30
5.forEach()---对数组的每个元素执行一次提供的函数
const emotions = ['happy', 'sad', 'angry'];
emotions.forEach( emotion => console.log(emotion));
// 'happy'
// 'sad'
// 'angry'
6.some()方法---判断数组中的某些元素是否通过由提供的函数实现的测试。一个很有用的实例就是检查用户的权限。
const userPrivileges = ['user', 'user', 'user', 'admin'];
const containsAdmin = userPrivileges.some( element => element === 'admin');
7.every()---检查是否数组中每个值都满足要求
const ratings = [3, 5, 4, 3, 5];
const goodOverallRating = ratings.every( rating => rating >= 3 );
// true
8.includes()---检查是否一个数组包含一个确定的值
const names = ['sophie', 'george', 'waldo', 'stephen', 'henry'];
const includesWaldo = names.includes('waldo');
9.Objects.values()---返回一个由定对象自己的所有可枚举属性值的数组
const icecreamColors = {
chocolate: 'brown',
vanilla: 'white',
strawberry: 'red',
}
const colors = Object.values(icecreamColors);
// ["brown", "white", "red"]
10.Objects.keys()---返回一个由定对象的自身可枚举属性组成的数组
const icecreamColors = {
chocolate: 'brown',
vanilla: 'white',
strawberry: 'red',
}
const types = Object.keys(icecreamColors);
// ["chocolate", "vanilla", "strawberry"]
11.Object.enntries()--返回一个由一个给定对象的键值对组成的数组
const weather = {
rain: 0,
temperature: 24,
humidity: 33,
}
const entries = Object.entries(weather);
// [['rain', 0], ['temperature', 24], ['humidity', 33]]
12.扩展运算符
const spreadableOne = [1, 2, 3, 4];
const spreadableTwo = [5, 6, 7, 8];
const combined = [...spreadableOne, ...spreadableTwo];
// [1, 2, 3, 4, 5, 6, 7, 8]
13.Object spread----扩展对象允许为一个没有更改的对象添加新的属性和方法(换句话说,创建了一个新对象)。对象扩展符也可以把多个对象合并在一起。注意,该方法不适合嵌套对象的复制
const spreadableObject = {
name: 'Robert',
phone: 'iPhone'
};
const newObject = {
...spreadableObject,
carModel: 'Volkswagen'
}
// { carModel: 'Volkswagen', name: 'Robert', phone: 'iPhone' }
14.Object.assign()---允许将对象组合在一起
const firstObject = {
firstName: 'Robert'
}
const secondObject = {
lastName: 'Cooper'
}
const combinedObject = Object.assign(firstObject, secondObject);
// { firstName: 'Robert', lastName: 'Cooper' }
15.join() 数组转字符串
let a= ['hello','world'];
let str=a.join(); // 'hello,world'
let str2=a.join('+'); // 'hello+world'
在数组操作中,以下会改变原数组: