当前位置:网站首页>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 :
边栏推荐
猜你喜欢
疫情、失业,2022,我们高喊着摆烂和躺平!
Redis cluster data skew
Background management of uniapp hot update
Jenkins自动化部署,连接不到所依赖的服务【已解决】
uniapp 热更新后台管理
什么是SRE?一文详解SRE运维体系
MySQL 因字符集问题插入中文数据时提示代码 :1366
数据中台:数据采集和抽取的技术栈详解
K8S部署高可用postgresql集群 —— 筑梦之路
ZUCC_ Principles of compiling language and compilation_ Experiment 0607 grammar analysis ll analysis
随机推荐
Base64编码详解及其变种(解决加号在URL变空格问题)
Application of tidb in Netease games
数据中台:数据中台全栈技术架构解析,附带行业解决方案
Smart power plant: how to make use of easycvr to build a safe, stable, green and environment-friendly intelligent inspection platform
opencv最大值滤波(不局限于图像)
剑指 Offer 55 - I. 二叉树的深度-dfs法
110. 平衡二叉树-递归法
[10 day SQL introduction] Day2
基于单片机开发的酒精浓度测试仪方案
基于QingCloud的地理信息企业研发云解决方案
every()、map()、forEarch()方法。数组里面有对象的情况
Variable declaration and some special variables in shell
There was an error checking the latest version of pip
什么是SRE?一文详解SRE运维体系
MySQL | 存储《康师傅MySQL从入门到高级》笔记
Earthly container image construction tool -- the road to dream
Several schemes of PHP code encryption
数据库迁移从PostgreSQL迁移到 MYSQL
Using skills of xargs -- the way to build a dream
数组相向指针系列