当前位置:网站首页>JS to find and update the specified value in the object through the key
JS to find and update the specified value in the object through the key
2022-06-24 08:48:00 【Front end tripod】
Preface
stay js Most of the data values of object types in obj.a.b.c.d… In this way, the value is taken , However, sometimes the data levels are nested very deeply, and it is very inconvenient to take values every time , So simply encapsulate two small methods for getting and updating values .
1、 adopt key Get the specified value in the object
/**
* adopt key Get the specified value of the object
* @param {*} obj Object of value
* @param {*} objKey After splicing key data ,string ‘>’ Symbol splicing
* @return {*} Return found value
*/
const getObjDataByKey = (obj, objKey) => {
const keyList = objKey.split('>');
return keyList.reduce((pre, item) => pre[item], obj);
}
const data = {
a: {
b: {
c: {
d: [1,2,3]
}
}
}
}
console.log(getObjDataByKey(data, 'a>b>c>d'));
The above code passes in two parameters ( Raw data and through > Spliced key value ), And then by intercepting > obtain key Of list data , Finally through keyList Of reduce Method gets one layer at a time obj Data in , Until finally get the last specified value and return . The printing effect is as follows :

2、 adopt key to update obj Specified data in
/**
* adopt key to update obj Specified data in
* @param {*} obj Objects that update values
* @param {*} objKey After splicing key data ,string ‘>’ Symbol splicing
* @param {*} newValue Updated value
* @return {*} Return the updated data
*/
const updateObjDataByKey = (obj, objKey, newValue) => {
const keyList = objKey.split('>');
const lasteKey = keyList[keyList.length - 1];
keyList.reduce((pre, item) => {
if (item === lasteKey) pre[item] = newValue;
return pre[item];
}, obj);
return obj;
}
const data = {
a: {
b: {
c: {
d: [1,2,3]
}
}
}
}
console.log(updateObjDataByKey(data, 'a>b>c>d', [555555555555]));
The above code passes in three parameters ( Raw data 、 adopt > Spliced key value 、 The latest value ), First by intercepting > obtain key Of list data , Finally through keyList Of reduce Method gets one layer at a time obj Data in , Judge when loop to keyList The last level will newValue Assigned to the current data , Finally, return the passed in object ( The intermediate assignment takes advantage of the characteristics of the reference data type ). The printing effect is as follows :

边栏推荐
- leetcode 1642. Furthest Building You Can Reach(能到达的最远的建筑)
- Earthly container image construction tool -- the road to dream
- Send custom events in QT
- Application of tidb in Netease games
- Using skills of xargs -- the way to build a dream
- The form image uploaded in chorme cannot view the binary image information of the request body
- Liunx change the port number of vsftpd
- Telnet port login method with user name for liunx server
- GradScaler MaxClipGradScaler
- Glusterfs replacement failure brick
猜你喜欢

基于QingCloud的 “房地一体” 云解决方案

every()、map()、forEarch()方法。数组里面有对象的情况

It is enough to read this article about ETL. Three minutes will let you understand what ETL is

Centos7 installation of jdk8, mysql5.7 and Navicat connection to virtual machine MySQL and solutions (solutions to MySQL download errors are attached)
![Jenkins is deployed automatically and cannot connect to the dependent service [solved]](/img/fe/f294955a9bdf7492aab360e44e052d.png)
Jenkins is deployed automatically and cannot connect to the dependent service [solved]

Database migration from PostgreSQL to MySQL

数据中台:数据采集和抽取的技术栈详解

【量化投资】离散傅里叶变换求数组周期

分布式 | 如何与 DBLE 进行“秘密通话”

表单图片上传在Chorme中无法查看请求体的二进制图片信息
随机推荐
2020中国全国各省市,三级联动数据,数据机构(数据来自国家统计局官网)
疫情、失业,2022,我们高喊着摆烂和躺平!
[force deduction 10 days SQL introduction] Day3
2022春招面试总结
Battle history between redis and me under billion level traffic
数据中台:数据中台全栈技术架构解析,附带行业解决方案
Ordering of MySQL composite index
数据中台:中台架构及概述
Introduction to data platform
pymysql 向MySQL 插入数据无故报错
Qt源码分析--QObject(2)
Centos7安装jdk8以及mysql5.7以及Navicat连接虚拟机mysql的出错以及解决方法(附mysql下载出错解决办法)
基于单片机开发的酒精浓度测试仪方案
[10 day SQL introduction] Day2
原生小程序用画布制作海报,等比例缩放,和uniapp差不多就是写法有点不同
Liunx change the port number of vsftpd
定时备份数据库脚本
获取屏幕宽高工具类
Shell basic operator -- arithmetic operator
数据中台:数据中台技术架构详解