一、concat()
var array1 = ["S60", "S70"];
var array2 = ["XC40", "XC50", "XC60"];
var array3 = ["t140", "t250", "Xt360"];
let newArray = array1.concat(array2,array3)
console.log('@newArray===',newArray);
二、copyWithin()
- 将数组元素复制到数组中的另一个位置,覆盖现有值
- params1 => 要改变的值的索引
- params2 => 目标值
- 会改变原数组
const fruits = ["Banana", "Orange", "Apple", "Mango"];
let newArray = fruits.copyWithin(0,3)
console.log('@newArray===',newArray);
三、entries()
- 返回带有键值对的 Array 迭代对象
- 通过 Object.entries() 调用
const fruits = ["Banana", "Orange", "Apple", "Mango"];
const arr2 = [{ a: 1 }, { b: 2 }, { c: 3 }];
let newArray = Object.entries(fruits)
console.log('@arr2',Object.entries(arr2))
console.log('@newArray===',newArray);
四、every()
const ages = [32, 33, 16, 40];
let flag = ages.every(item => item > 15)
console.log('@flag===',flag);
五、fill()
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.fill("Kiw",0,1);
console.log('@fruits',fruits);
六、filter()
- 过滤出 符合某个条件( age > 18 ) 的 值
var ages = [32, 33, 16, 40];
let newArr = ages.filter(item =>{
return item >= 18
})
console.log('@',newArr);
七、find()
var ages = [3, 10, 18, 20];
let val = ages.find(item =>{
return item > 3
})
console.log(val);
八、findIndex()
var ages = [3, 10, 18, 20];
let val = ages.findIndex(item =>{
return item > 3
})
console.log('@索引值',val);
九、forEach()
- 没有返回值
- forEach适合于你并不打算改变数据的时候,而只是想用数据做一些事情 – 比如存入数据库
- forEach()允许callback更改原始数组的元素。map()返回新的数组。
let arr =[1,2,3,4,5,6]
arr.forEach((value, key) => {
return arr[key] = value * value;
});
console.log('@arr',arr)
十、from()
- ,Array.from() 方法对一个类似数组或可迭代对象创建一个新的,浅拷贝的数组实例。可以将两种对象转换成数组
let arr1 = Array.from('foo');
console.log('@arr1',arr1);
let arr2 = Array.from(['foo', 'bar', 'baz', 'foo'])
console.log('@arr2',arr2);
let arr3 = Array.from([[1, 2], [2, 4], [4, 8]])
console.log('@arr3',arr3);
let arr4 = new Map([['1', 'a'], ['2', 'b']])
console.log(Array.from(arr4.values()));
console.log(Array.from(arr4.keys()));
function f() {
return Array.from(arguments);
}
console.log('@类数组对象',f(1, 2, 3));
let arr6 = Array.from([1,2,3],x => x + x)
console.log('@arr6',arr6);
let arr7 = Array.from({length:5},(v,i) => i)
console.log('@arr7',arr7);
const range = (start, stop, step) => Array.from(
{ length: (stop - start) / step + 1},
(_,i) => start + (i * step)
);
range(0, 4, 1);
console.log('@range',range(0, 4, 1));
let zimu = range('A'.charCodeAt(0), 'Z'.charCodeAt(0), 1).map(x => String.fromCharCode(x));
console.log('@zimu',zimu);
function combine(){
let arr = [].concat.apply([],arguments)
return Array.from(new Set(arr))
}
var m = [1,2,3,2], n = [2,3,3,4]
console.log('@去重后的数组并且合并',combine(m,n));
let array = [1,1,1,4,5,6,3]
console.log('@set去重',[...new Set(array)]);
let arr10 = [
{id:1,groupCode:1,name:'test01'},
{id:2,groupCode:1,name:'test02'},
{id:3,groupCode:2,name:'test03'},
{id:4,groupCode:3,name:'test04'},
{id:5,groupCode:3,name:'test05'},
{id:6,groupCode:5,name:'test06'},
]
function groupFn(arr){
let map = {}
arr.map((item) =>{
if(!map[item.groupCode]){
map[item.groupCode] = [item]
}else {
map[item.groupCode].push(item)
}
})
console.log('@map',map);
let resData = [];
Object.keys(map).forEach((key) => {
console.log('@key',key);
resData.push({
groupCode: key,
data: map[key],
});
});
console.log('@resData',resData);
}
groupFn(arr10)
十一、includes()
const arr = ['小明', 'a', 'b', 'c']
arr.includes('a')
arr.includes('小明')
arr.includes('a', 1)
arr.includes('a', 2)
arr.includes('小明', 1)
arr.includes('a', -1)
arr.includes('a', 100)
十二、indexOf()
- 方法在数组中搜索指定项目,并返回其位置。
- 参数1:value 目标值 找到了 返回 值所在的
索引值 ,没有找到 返回 -1 - 参数2: index:可选参数,开始查找的下标,默认从0开始查找,如果匹配到就返回值所在的位置,没有匹配到返回-1。
const arr = ['小明', 'a', 'b', 'c']
arr.indexOf('b')
arr.indexOf('小美')
arr.indexOf('小明')
arr.indexOf('a', 1)
arr.indexOf('小明', 2)
arr.indexOf('小明', 0)
arr.indexOf('c', -1)
arr.indexOf('a', 100)
const value="青少年都是祖国的花朵!"
value.indexOf('花朵')
value.indexOf('鸟')
value.indexOf('都是')
十三、isArray()
let arr1 = Array.isArray([])
let arr2 = Array.isArray([1])
let arr3 = Array.isArray(new Array())
let arr4 = Array.isArray()
let arr5 = Array.isArray({})
let arr6 = Array.isArray(null)
let arr7 = Array.isArray(undefined)
let arr8 = Array.isArray(17)
let arr9 = Array.isArray('Array')
console.log(arr1);
十四、join()
var fruits = ["Banana", "Orange", "Apple", "Mango"];
console.log(fruits.join());
十五、keys()
- 创建一个 Array Iterator 对象,只包含对象的键,然后遍历每个键:
- 获取 对象中 每个的
key 值 - 返回值 是 一个数组
const object1 = {
a: 'somestring',
b: 42,
c: false
};
console.log('@@@',Object.keys(object1));
十六、lastIndexOf()
- // 返回一个指定的字符串值最后出现的位置,在一个字符串中的指定位置从后向前搜索
var str = 'xxaxxaxxax';
var endA2 = str.lastIndexOf('a',7)
console.log(endA2);
十七、map()
let arr =[1,2,3,4,5,6]
let list = arr.map(value => {
return value * value;
});
console.log('@list',list)
十八、pop()
const fruits = ["Banana", "Orange", "Apple", "Mango"];
let arr = fruits.pop()
console.log('@arr',arr);
console.log('@fruits11111',fruits);
十九、push()
const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push('张三')
console.log('@fruits',fruits);
二十、reduce()
- reduce函数可以接受接受两个参数,一个为函数,一个为初始值,
var test = [1, 2, 3, 4, 5];
var total = 0;
total = test.reduce((prevalue, item) => {
return item + prevalue;
}, 0);
console.log(total);
二十一、reduceRight()
var arr = [0,1,2,3,4];
arr.reduceRight(function (preValue,curValue,index,array) {
return preValue + curValue;
});
二十二、shift()
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.shift()
console.log('@fruits',fruits);
二十三、slice()
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = fruits.slice(1, 3);
console.log('@citrus',citrus);
二十四、some()
- 方法检查数组中的任何元素是否通过测试
- 只要有一个 元素 符合就 返回true
var ages = [3, 10, 18, 20];
let flag = ages.some(item =>{
return item > 18
})
console.log('@flag',flag);
二十五、sort()
var arr = ['General','Tom','Bob','John','Army'];
var resArr = arr.sort();
console.log(resArr);
var arr2 = [30,10,111,35,1899,50,45];
var resArr2 = arr2.sort();
console.log(resArr2);
var arr3 = [30,10,111,35,1899,50,45];
arr3.sort(function(a,b){
return a - b;
})
console.log(arr3);
var arr4 = [30,10,111,35,1899,50,45];
arr4.sort(function(a,b){
return b - a;
})
console.log(arr4);
var arr5 = [{id:10},{id:5},{id:6},{id:9},{id:2},{id:3}];
arr5.sort(function(a,b){
return a.id - b.id
})
console.log(arr5);
var arr6 = [{id:10,age:2},{id:5,age:4},{id:6,age:10},{id:9,age:6},{id:2,age:8},{id:10,age:9}];
arr6.sort(function(a,b){
if(a.id === b.id){
return b.age - a.age
}else{
return a.id - b.id
}
})
二十六、splice()
var arr = ["aa","bb","cc","dd"];
var arr1 = arr.splice(0,3);
console.log(arr1);
console.log(arr);
var arr = ["aa","bb","cc","dd"];
var arr1 = arr.splice(1,0,"ff","pp");
console.log(arr1);
console.log(arr);
var arr = ["aa","bb","cc","dd"];
var arr1 = arr.splice(1,2,"ff","pp");
console.log(arr1);
console.log(arr);
二十七、toString()
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var x = fruits.toString();
console.log('@',x);
二十八、unshift()
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.unshift('99');
console.log('@',fruits);
二十九、valueOf()
- valueof()方法:它就默认将对象转换为表示它的原始值。
var arr = [1,2,3]
console.log(arr.valueOf());
console.log(typeof arr.valueOf());
console.log(arr.valueOf() instanceof Array);
console.log(arr.toString());
console.log(typeof arr.toString());
var obj ={
a:1
}
console.log('===========');
console.log(obj.valueOf());
console.log(typeof obj.valueOf());
console.log(obj.valueOf() instanceof Object);
console.log(obj.toString());
console.log(typeof obj.toString());
var date = new Date()
console.log('============');
console.log(date.valueOf());
console.log(typeof date.valueOf());
console.log(date.toString());
var reg = new RegExp(/\d/)
console.log('=============');
console.log(reg.valueOf());
console.log(typeof reg.valueOf());
console.log(reg.toString());
console.log(typeof reg.toString());
|