js数组对象等使用方法,防忘记!

102 阅读4分钟

Array

1.concat()合并多个数组,原数组不变,返回新的数组,也可用于字符串

const a = [1, 2, 3]
const b = [4, 5, 6]
const c = a.concat(b)
console.log(c) // [1, 2, 3, 4, 5]

2.join() 把数组的所有元素放入一个字符串。元素通过指定的分隔符进行分隔

const arr = ["苹果","桃子","西瓜"];
console.log(arr.join('、')); // 苹果、桃子、西瓜

3.push() 向数组的末尾添加一个或更多元素,并返回新的长度,改变原数组。

const a = [1, 2, 3]
const b = a.push(4, 5)
console.log(b) // 5 (返回的是数组的length)
console.log(a) // [1, 2, 3, 4, 5]

4.unshift() 向数组的开头添加一个或更多元素,并返回新的长度,改变原数组。

const a = [1, 2, 3]
const b = a.unshift(0)
console.log(b) // 4 (返回的是数组的length)
console.log(a) // [0, 1, 2, 3]

5.shift() 把数组的第一个元素从其中删除,并返回第一个元素的值,改变原数组。

const a = [1, 2, 3]
const b = a.shift()
console.log(b) // 1
console.log(a) // [2, 3]

6.sort() 对数组元素进行排序,改变原数组。

const arr = [5, 3, 4, 1, 2, 2]
console.log(arr.sotr()) // [1, 2, 2, 3, 4, 5]

7.reverse() 颠倒数组中元素的顺序,改变原数组。

const arr = [1, 2, 3, 4, 5]
console.log(arr.reverse()) // [5, 4, 3, 2, 1]

8.pop() 删除并返回数组的最后一个元素,改变原数组

const a = [1, 2, 3]
const b = a.pop()
console.log(b) // 3
console.log(a) [1, 2]

9.slice(start, end) 用于提取原来数组的一部分,会返回一个提取的新数组,原数组不,不包括end。

const arr = [0, 1, 2, 3, 4, 5]
console.log(arr.slice(1,3)) // [1, 2]

10.splice(t, v, s) 用于删除原数组的一部分,并且可以在删除的位置添加新的数组成员,返回值是被删除的数组元素, 改变原数组。t:被删除元素的起始位置;v:被删除元素个数;s:s以及后面的元素为被插入的新元素。

const a = [0, 1, 2, 3, 4, 5, 6]
const b = a.splice(3, 3, 'new')
console.log(b) // [3, 4, 5]
console.log(a) // [0, 1, 2, 'new', 6]

11.toString() 把数组转换为字符串,并返回结果

const arr = ['one', 'two', 'three']
console.log(arr.toString()) // one,two,three

12.map() 依次遍历数组成员,根据遍历结果返回一个新数组。

const arr = [1, 2, 3, 4, 5]
console.log(arr.map(item=>item*2)) // [2, 4, 6, 8, 10]

13.forEach() 跟map方法类似,遍历数组,区别是无返回值。

const arr = [1, 2, 3, 4, 5]
arr.forEach((item,index)=>{
	console.log(item) // 1 
			  // 2
                          // ...	
})

14.for in() 跟map方法类似,遍历对象或者数组。 但是for in循环返回的值都是数据结构的键值名。遍历对象返回的对象的key值,遍历数组返回的数组的下标(key)。

// 对象	返回键值
const obj = {a: 1, b: 2, c: 3 }
for (let i in obj) {
console.log(i)	// a
		// b
		// c
}

//数组 返回下标
const arr = [1, 2, 3]
for(let i in arr) {
console.log(i)	// 0
		// 1
		// 2
}

15.filter() 它用于把Array的不符合条件的元素过滤掉,然后返回结果为true的元素,不会改变原数组。

const arr = [1, 2, 3, 4, 5]
console.log(arr.filter(item => item>2)) // [3, 4, 5]

16.indexOf() 返回元素在数组中的第一次出现的位置的下标,如果没有则返回-1。

const arr = [1, 1, 2, 3]
console.log(arr.indexOf(1)) // 0
console.log(arr.indexOf(5)) // -1

17.lastIndexOf() 返回元素在数组中最后一次出现的位置,没有返回-1。

const arr = [1, 1, 2, 1, 3]
console.log(arr.indexOf(1)) // 3
console.log(arr.indexOf(5)) // -1

18.flatten() 简写为flat(),接收一个数组,无论这个数组里嵌套了多少个数组,flatten最后都会把其变成一个一维数组(扁平化)

const a = [[1,2,3],[4,5,[6,7]]];
const b = a.flatten(3);
console.log(b); // [1, 2, 3, 4, 5, 6, 7]

19.new Set() 数组去重

const arr = [1, 2, 2, 3, 3, 4, 5]
console.log(new Set(arr)) // {1, 2, 3, 4, 5}
const a = Array.from(new Set(arr)) // [1, 2, 3, 4, 5]

20.find() 返回符合条件的数组元素。

const arr = [1, 2, 3, 4, 5]
const a = test.find(item => item > 2)
console.log(a) // 3 (返回通过测试的第一个元素的值。)
const b = test.find(item => item == -10)
console.log(b) //undefined(如果没有符合条件的元素返回 undefined)

21.includes(v,t) es7新增,返回一个布尔值,表示某个数组是否包含给定的值。 可选第二参数 代表搜索的起始位置,默认为0,负数代表倒数的位置

const arr = [1, 2, 3, 4, 5]
console.log(arr.includes(2)) // true
console.log(arr.includes(100)) // false

22.every() 判断数组中每一项是否都满足条件,只有所有项都满足条件,才会返回true。

const a = [1, 2, 3, 4, 5, 6]
const b = a.every(item=>{
	return item > 0
})
console.log(b) // true

23.some() 判断数组中只要任意一项满足条件,就会返回true

const a = [1, 2, 3, 4, 5, 6]
const b = a.some(item=>{
	return item > 5
})
console.log(b) // true

24.Array.isArray() 判断是否为数组

const arr = [1, 2, 3, 4, 5, 6]
console.log(Array.isArray(arr)) // true 

String

1.split() 将字符串切割成数组。

const str = 'hello';
console.log(str.split('')) // ["h", "e", "l", "l", "o"]
console.log(str.split('', 3)) //  ["h", "e", "l"]

2.replace() 替换匹配的字符串。

const str = 'hello 王力宏'
console.log(str.replace('王力宏', '周杰伦'))  // hello 周杰伦

3.charAt() 返回指定位置的字符。

const str = 'javaScript'
console.log(str.charAt(3))  // a

4.split() 将字符串切割成数组。

const str = 'Jay';
console.log(str.split()) // ["Jay"]
console.log(str.split('', 2)) //  ["J", "a"]

5.toLocaleUpperCase() && toUpperCase() 将字符串转换成大写。

const str = 'jay';
console.log(str.toLocaleUpperCase())  // JAY
console.log(str.toUpperCase())  // JAY

6.trim() 去除字符串两端的空格

const str = '  jay   ';
console.log(str.trim()) // jay(不会改变原数组)