JS常用字符串、数组方法

您所在的位置:网站首页 樱岛麻衣的死对头是谁呀 JS常用字符串、数组方法

JS常用字符串、数组方法

2023-07-02 19:17| 来源: 网络整理| 查看: 265

pop() 用于删除并返回数组中的最后一个元素

var temp = ["张三", "李四", "王五"]; temp.pop() // ["张三", "李四"]

push(添加元素1..添加元素n) 向数组末尾添加一个或多个元素,并返回数组的长度

var art = ['a', 'b', 'c']; art.push('d', 'e') //["a", "b", "c", "d", "e"] ​ // 1.去除数组中小于等于100的数 let list = [12, 156, 46, 174, 16, 100]; let newList = []; for (let i = 0; i < list.length; i++) { if (list[i] return b-a // 后减前降序,前减后升序

cancat 合并两个或多个数组

let a = [1, 2, 3]; let b = [4, 5, 6]; ​ //连接两个数组 a.concat(b); // [1,2,3,4,5,6]

扩展运算符...合并数组

let a = [2, 3, 4, 5] let b = [ 4,...a, 4, 4] // [4,2,3,4,5,4,4]

forEach 遍历数组中的所有元素。 此函数将为每个数组元素调用回调函数,并将参数传递给它。

// forEach方法中的function回调有三个参数:第一个参数是遍历的数组内容,第二个参数是对应的数组索引,第三个参数是数组本身 let arr = ['apple', 'banana', 'organge']; arr.forEach(function (val, index, array) { console.log(val, index, array, this); }, 123) // 指向this指向123

every 检测数组所有元素是否都符合判断条件,如果数组中检测到有一个元素不满足, 则整个表达式返回false,且后续不会再进行检测

function isBigEnough(element, index, array) { ​ return element >= 10; // 判断数组中的所有元素是否都大于10 ​ } ​ [12, 5, 8, 130, 44].every(isBigEnough); // false ​ [12, 54, 18, 130, 44].every(isBigEnough); // true ​ // 接受箭头函数写法 ​ [12, 5, 8, 130, 44].every(x => x >= 10); // false ​ [12, 54, 18, 130, 44].every(x => x >= 10); // true ​ ​ const arr = [ { id: 1, name: "西瓜", state: true }, { id: 2, name: "栗子", state: false }, { id: 3, name: "草莓", state: true }, ] // 每一个勾选都为true返回true let result = arr.every(item => item.state); ​ ​ let arrEvery = [1, 2, 5, 4, 9]; let res = arrEvery.every((val, index, arr) => { return val % 2 == 1 // 判断奇数 }) // false

some 查询数组中的是否有满足判断条件的元素,如果有一个元素满足条件,则表达式返回true, 剩余的元素不会再执行检测

some方法 只要其中一个为true 就会返回true的,相反,every()方法必须所有都返回true才会返回true

arr.some((item, index) => { if (item === '小米') { //找到对应的项之后,可以通过return true来终止循环 return true } }) ​ ​ let arrSome = ['apple', 'banana', 'organge']; let b = arrSome.some((val, index, arr) => { return val == 'apple' })

fliter 方法创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素。

let newNums = nums.filter(function (n) { return n < 100; }) ​ // 去除数组中小于等于100的数 let list = [12, 156, 15, 46, 175, 17, 100]; let newList = []; // 为true的时候,将符合条件的元素返回,而不是将true返回(过滤) let newList = list.filter(function (n) { return n item.state).reduce((累加的结果,当前循环项)=>{},初始值) arr.filter(item => item.state).reduce((amt, item) => { return amt += item.price * item.count }, 0) ​ arr.filter(item => item.state).reduce((amt, item) => (amt += item.price * item.count), 0) ​ ​ // flter过滤器 let arr2 = [{ title: 'aaa', reds: true }, { title: 'bbb', reds: false }] let myflut = arr2.filter((val) => { return val.reds }) // [ { title: 'aaa', reds: true } ]

map 返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值。

// 将newLIst所有值乘于2 let b = list.map(function (n) { return n * 2 }) ​ // 不加return相当于forEach,加上return返回一个数组 let arr = ['apple', 'banana', 'organge']; let myrander = arr.map((val, index, arr) => {  return (val) }) ​ ​ // 修改数据结构 let arr2 = [{ title: 'aaa', reds: 100 }, { title: 'bbb', reds: 100 }] let myflut = arr2.map((val) => {  let json = { t: val.title, r: val.reds + 100 }  return json }) // [ { t: 'aaa', r: 200 }, { t: 'bbb', r: 200 } ]

reduce 接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值。

// reduce() 方法接受四个参数:初始值(或者上一次回调函数的返回值),当前元素值,当前索引,调用 reduce() 的数组。 // reduceRight() 方法的功能和 reduce() 功能是一样的,不同的是 reduceRight() 从数组的末尾向前将数组中的数组项做累加。 let arr = [2, 3]; let arrReduce = arr.reduce((prev, cur, index, arr) => { // return prev + cur //求和 // Math.pow(2,3) ==> 2 ** 3 return Math.pow(prev, cur);//阶乘 }) console.log(arrReduce); // 8 ​ ​ ​ // 数组求和 // 将一个数组从第一元素开始累加/累乘,最终得到一个总和 // pre上一次累加的结果,next下一个值,0:0开始 let sum = list.reduce(function (pre, next) { return pre + next }, 0) ​ ​ // 将二维数组转化为一维 let flattened = [[0, 1], [2, 3], [4, 5]].reduce( (a, b) => a.concat(b), [] );// [0, 1, 2, 3, 4, 5] ​ ​ // reduce方法用于对一些数据进行累计计算求和 const arr = [ { id: 1, name: "西瓜", state: true, price: 10, count: 2 }, { id: 2, name: "栗子", state: false, price: 100, count: 1 }, { id: 3, name: "草莓", state: true, price: 105, count: 1 }, ] let amt = 0;//总价 arr.filter(item => item.state).forEach(item => { amt += item.price * item.count }) ​ ​ // in判断某个属性是否属于某个数组或对象。 let names = ['tom', 'jim', 'jack', 'tom', 'jack']; let countNames = names.reduce((allNames, name) => {  if (name in allNames) {    allNames[name]++; }  else {    allNames[name] = 1; }  return allNames; }, {}); console.log(countNames)  // { tom: 2, jim: 1, jack: 2 }

find()& findIndex() 根据条件找到数组成员

[1, 4, -5, 10,NaN].find((n) => Object.is(NaN, n)) // NaN ​ // find() 方法返回通过测试(函数内判断)的数组的第一个元素的值。没有返回underfined 有就返回第一次对应的值 let arr = [50, 100, 900, 300, 200, 100, 900, 300, 200]; let arrFind = arr.find((val, index, arr) => { return val > 99 }) console.log(arrFind); // 100 返回符合第一次条件的值 ​ let arr1 = [50, 100, 900, 300, 200]; let arrFind1 = arr1.find(v => v === 200) console.log(arrFind1); // 200 返回符合第一次条件的值 ​ let arr = [{"goods_id":1,"name":'zs'},{"goods_id":2,"name":'ls'}] const findResult = arr.find(item=>item.goods_id === 1) console.log(findResult) // {goods_id: 1, name: 'zs'} ​ ​ // findIndex() 方法返回传入一个条件(函数)符合条件的数组第一个元素位置。没有返回-1 返回值第一次对应所在的索引 let arr2 = [50, 100, 900, 300, 200, 100, 900, 300, 200]; let arrFind2 = arr2.findIndex(v => v === 200) console.log(arrFind2); // 4 ​ [1, 4, -5, 10].findIndex((n) => n < 0) // 返回索引2

copyWithin() 指定位置的成员复制到其他位置

let a = ['zhang', 'wang', 'zhou', 'wu', 'zheng']; ​ // 1位置开始被替换, 2位置开始读取要替换的 5位置前面停止替换 ​ a.copyWithin(1, 2, 5); ​ // ["zhang", "zhou", "wu", "zheng", "zheng"]

fill() 填充数组

['a', 'b', 'c'].fill(7) // [7, 7, 7] ​ ['a', 'b', 'c'].fill(7, 1, 2) // ['a', 7, 'c'] ​ // fill() 方法用于将一个固定值替换数组的元素。 array.fill(value, start, end) let arr = new Array(3); arr.fill('默认值', 0, 3); console.log(arr); // ['默认值', '默认值', '默认值'] ​ let arr = [1, 2, 3, 4, 5]; arr.fill('默认值', 0, 3); console.log(arr); // ) ['默认值', '默认值', '默认值', 4, 5]

keys()&values()&entries() 遍历键名、遍历键值、遍历键名+键值

for (let index of ['a', 'b'].keys()) {  console.log(index); } // 0 // 1 ​ for (let elem of ['a', 'b'].values()) {  console.log(elem); } // 'a' // 'b' ​ ​ for (let [index, elem] of ['a', 'b'].entries()) {  console.log(index, elem); } // 0 "a" // 1 "b"


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3