码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • js(javascript)中关于查找与替换常用的实用方法


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

    文章目录

    • js(javascript)中关于查找与替换常用的实用方法
      • 判断有无
        • 判断某字符串内是否含有某个字符或者字符串
        • 判断某数组内是否含有某个字符串
      • 查找通过某值查找某值
        • 在数组对象中寻找某个item的value等于某值的item
      • 替换
        • 替换某个字符串中的某些字符为其他字符
        • 替换某个数组对象中的某些键名为指定的键名

    判断有无

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

    例:在“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()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

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

    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()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    查找通过某值查找某值

    在数组对象中寻找某个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 : ''
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    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)//李斯
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    替换

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

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

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

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

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

    let json = [
        {
            name: '张三',
            id:'1'
        },
        {
            name: '李斯',
            id:'2'
        },
        {
            name: '旺屋',
            id:'3'
        },
    ]
    // 转换后的结果
    
    let json = [
        {
            label: '张三',
            value:'1'
        },
        {
            label: '李斯',
            value:'2'
        },
        {
            label: '旺屋',
            value:'3'
        },
    ]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    let data = JSON.parse(JSON.stringify(json).replace(/"name"/g, '"label"'))
    data = JSON.parse(JSON.stringify(data).replace(/"id"/g, '"value"'))
    console.log(data)
    // 注意:这种方法可以替换多层数据
    
    • 1
    • 2
    • 3
    • 4
  • 相关阅读:
    内核APC&用户APC详解
    台式机window11安装ubuntu18.04后找不到wifi(网卡型号RTL8822CE)
    mysql explain extra 信息分析
    Java网络编程——BIO阻塞IO
    基于新型战争策略优化算法的光伏模型优化(Matlab代码实现)
    根据前序中序求后序
    【统计学】Top-down自上而下的角度模型召回率recall,精确率precision,特异性specificity,模型评价
    Redis数据类型-List-基本使用
    3_python高阶_线程—多线程-共享全局变量
    这是我见过最详细易懂的Redis笔记(PDF可下载),上线三天破百万点赞
  • 原文地址:https://blog.csdn.net/weixin_42708208/article/details/127480922
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号