js(javascript)中关于查找与替换常用的实用方法

js(javascript)中关于查找与替换常用的实用方法

判断有无

判断某字符串内是否含有某个字符或者字符串

例:在“hello,world” 里面是否有el

let str = "hello,world", str1 = 'l'
let idx=str.indexOf(str1)
if(idx!==-1){
  console.log('字符串'+str+'内存在'+str1+',下标位置为:'+idx)
} else {
    console.log('字符串'+str+'内未找到'+str1)
}
// 如果有多个时,会返回第一个字符(串)所在的位置
// 如果要找到最后一个,可以使用 lastIndexOf()

判断某数组内是否含有某个字符串

let arr = ["hello","world","china","beautiful"], obj = 'world'
let idx=arr.indexOf(obj)
if(idx!==-1){
  console.log('字符串'+arr+'内存在'+obj+',下标位置为:'+idx)
} else {
    console.log('字符串'+arr+'内未找到'+obj)
}
// 如果有多个时,会返回第一个字符(串)所在的位置
// 如果要找到最后一个,可以使用 lastIndexOf()

查找通过某值查找某值

在数组对象中寻找某个item的value等于某值的item

/**
 * 通过某个值找出其所在的item对象
 * @list 数组对象
 * @inKey 需要作为条件的key键
 * @inVal 需要作为条件的key值
 * @outVal 需要返回的结果的key键
 * 举例:在json中找id==2的name 
 * inKey='id'
 * inVal='2'
 * outVal='name'
 * 
 * let json = [
    {
        name: '张三',
        id:'1'
    },
    {
        name: '李斯',
        id:'2'
    },
    {
        name: '旺屋',
        id:'3'
    },
]  
 **/
findItemFromKey = function (list, inKey, inVal, outKey) {
    if ((!list) || list.length == 0) {
        console.error('数组对象不能为空')
		return '';
	}
    if (!(list instanceof Array)) {
        console.error('数组对象必须是数组')
		return '';
	}
    if (inKey == undefined || inKey == null) {
        console.error('被寻找键不能为空')
		return '';
	}
	if (inVal == undefined || inVal == null) { //此处可能会有0值
        console.error('被寻找键值不能为空')
		return '';
	}
	let data = list.find((item) => {
		return item[inKey] == inVal
    })
    if (outKey) {
        return data ? data[outKey] : ''
    } else {
        return data ? data : ''
    }
}
let json = [
    {
        name: '张三',
        id:'1'
    },
    {
        name: '李斯',
        id:'2'
    },
    {
        name: '旺屋',
        id:'3'
    },
]  
let out = findItemFromKey(json, 'id', 2)
console.log(out)//{ name: '李斯', id: '2' }
let out1 = findItemFromKey(json, 'id', 2,'name')
console.log(out1)//李斯

替换

替换某个字符串中的某些字符为其他字符

例:将hello,world内所有的l替换为#

let str = "hello,world"
str=str.replace(/l/gi, '#')
console.log(str)
// 注意,replace 不改变原来字符串,返回的是新的字符串,所以为了操作方便需要重新赋值,也可以直接返回得到的新值

替换某个数组对象中的某些键名为指定的键名

例:将后台返回的如下数据,转为我们需要的label+value结构

let json = [
    {
        name: '张三',
        id:'1'
    },
    {
        name: '李斯',
        id:'2'
    },
    {
        name: '旺屋',
        id:'3'
    },
]
// 转换后的结果

let json = [
    {
        label: '张三',
        value:'1'
    },
    {
        label: '李斯',
        value:'2'
    },
    {
        label: '旺屋',
        value:'3'
    },
]
let data = JSON.parse(JSON.stringify(json).replace(/"name"/g, '"label"'))
data = JSON.parse(JSON.stringify(data).replace(/"id"/g, '"value"'))
console.log(data)
// 注意:这种方法可以替换多层数据
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值