nodejs函数之数组篇

nodejs基础总结之数组篇

    • 数组函数
      • concat
      • 数组去重排序
      • reduce应用
      • filter应用
      • map的应用

数组函数


// 数组对象
arrayObject.slice(start,end) 方法可从已有的数组中返回选定的元素。
// 合并数组
list1.concat(list2)
// 数组拆分
array.join(',')
// 去除重复
checkArr = Array.form(new Set(checkIdArr))
// 往数组中添加元素
list1.push('西张明-辉');
// 往首位添加一个元素
list1.unshift('名称');


Object.keys(obj)

从数组 添加/删除项目(3个参数)
list1.splice(index:必须 整数 添加/删除项目的位置 为负则从结尾处规定位置,howmany:必须要删除的项目数量,item:可选 添加新项目)

// 从数组中删除一个元素 
list.splice(index, 1);

concat

concat() 方法用于合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组。

const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2);
// ["a", "b", "c", "d", "e", "f"]
console.log(array3);

数组去重排序


// 数组去重排序
return [...new Set([1,23,4,5,6,23,4,1])].sort((a,b)=>a-b);

reduce应用

//求出总(金额)数
let systemScore = ruleScore.reduce((prev, curr) => Number(prev) + Number(curr.score), 0);

filter应用

// 目的是剔除为0的数
return costArr.filter(item => item.count > 0);

map的应用

// 取出二维数组中所有的id
return [].concat(...doctorList.map(item => item.id))

你可能感兴趣的:(nodejs,node.js)