当前位置:网站首页>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 :

边栏推荐
- 【生活思考】计划与自律
- Ordering of MySQL composite index
- input的聚焦后的边框问题
- mysql组合索引的有序性
- Several schemes of PHP code encryption
- Smart power plant: how to make use of easycvr to build a safe, stable, green and environment-friendly intelligent inspection platform
- One development skill a day: how to establish P2P communication based on webrtc?
- Video Fusion communication has become an inevitable trend of emergency command communication. How to realize it based on easyrtc?
- Qt源码分析--QObject(2)
- 1844. 将所有数字用字符替换
猜你喜欢
随机推荐
Using sonar for code checking
2022.06.23(LC_144,94,145_二叉树的前序、中序、后序遍历)
rsync做文件备份
The pie chart with dimension lines can set various parameter options
利用ngrok做内网穿透
Application of tidb in Netease games
Background management of uniapp hot update
Detailed explanation of Base64 coding and its variants (to solve the problem that the plus sign changes into a space in the URL)
Base64编码详解及其变种(解决加号在URL变空格问题)
【力扣10天SQL入门】Day2
双指针模拟
数据中台:数据中台技术架构详解
String to Base64
Video Fusion communication has become an inevitable trend of emergency command communication. How to realize it based on easyrtc?
It is enough to read this article about ETL. Three minutes will let you understand what ETL is
数据中台:中台架构及概述
Lombok use
Centos7安装jdk8以及mysql5.7以及Navicat连接虚拟机mysql的出错以及解决方法(附mysql下载出错解决办法)
1528. 重新排列字符串
PHP代码加密的几种方案









