js将字符串数组转数值型数组-数组常用方法

String->Number 数组 a.map(Number)

['123','465'].map(Number)

//[123,456]

Number->String 数组 a.map(String)

[123,456].map(String)

//['123','465']

 字符串替换

var str = "my chian!"

let key = str.replace(/my/, "Mi");
console.log(key)

//输出:Mi chian!

找出数组中大小值

let num = [1,9,2,5,20,4]

let max = Math.max(...num)
let min = Math.min(...num)

console.log(max)//输出:20
console.log(min)//输出:1

去除数组中相同id值

let a = [
  { id: 1, name: 'aa' },
  { id: 2, name: 'bb' },
  { id: 3, name: 'cc' }
]

let b = [
  { id: 1, name: 'dd' },
  { id: 2, name: 'ff' },
]

let ary = a.filter(item => !b.some(ele => ele.id === item.id));
console.log(ary)

//输出   [{ id: 3, name: 'cc' }]

split 字符串分割=>数组

str='d|y|h'; 
arr=str.split('|');

join 数组=>字符串

['d', 'y', 'h'].join('|')

concat 两个数组连接

['d', 'y', 'h'].concat([1234])

charAt 返回指定位置的字符

var str='a,g,i,d,o,v,w,d,k,p'
console.log(str.charAt(2),'==========','charAt') // g 
//注:( , 也算一个字符)

charCodeAt 返回指定位置的字符的 Unicode 编码

var str='a,g,i,d,o,v,w,d,k,p'
console.log(str.charCodeAt(2),'==========','charCodeAt')

slice 截取子字符串

var str='ahji3o3s4e6p8a0sdewqdasj'
console.log(str.slice(2,5),'==========','slice')//ji3

substr 返回从指定位置开始指定长度的子字符串

var str = "0123456789";
console.log(str.substr(2,5),'==========','substr') // 23456

posted @ 2021-12-13 10:17  JackieDYH  阅读(149)  评论(0编辑  收藏  举报  来源