当前位置:网站首页>js中通过key查找和更新对象中指定值的方法
js中通过key查找和更新对象中指定值的方法
2022-06-24 06:59:00 【前端三脚猫】
前言
在js中对象类型的数据取值大多使用obj.a.b.c.d…这种方式取值,但是有时候数据的层级嵌套的非常深每次取值就会非常不方便,所以简单封装两个取值和更新值的小方法。
1、通过key获取对象中的指定值
/**
* 通过key获取对象指定值
* @param {*} obj 取值的对象
* @param {*} objKey 拼接后的key数据,string ‘>’符号拼接
* @return {*} 返回查找到的值
*/
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'));
上述代码传入两个参数 (原始数据和通过>拼接的key值),然后通过截取>获取key的list数据,最后通过keyList的reduce方法每次获取一层obj中的数据,直至最后获取最后指定的值返回。打印效果如下:

2、通过key更新obj中的指定数据
/**
* 通过key更新obj中的指定数据
* @param {*} obj 更新值的对象
* @param {*} objKey 拼接后的key数据,string ‘>’符号拼接
* @param {*} newValue 更新的值
* @return {*} 返回更新后的数据
*/
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]));
上述代码传入三个参数 (原始数据、通过>拼接的key值、最新的值),首先通过截取>获取key的list数据,最后通过keyList的reduce方法每次获取一层obj中的数据,判断当循环到keyList最后一级时将newValue赋值给当前数据,最后返回传入的对象即可(中间的赋值利用了引用数据类型的特点)。打印效果如下:

边栏推荐
- Three categories of financial assets under the new standards: AMC, fvoci and FVTPL
- 貸款五級分類
- DHCP, TFTP Foundation
- longhorn安装与使用
- 487. 最大连续1的个数 II ●●
- 独立站运营中如何提升客户留存率?客户细分很重要!
- OpenCV to realize the basic transformation of image
- 2021-03-11 COMP9021第八节课笔记
- 487. number of maximum consecutive 1 II ●●
- ZUCC_编译语言原理与编译_实验05 正则表达式、有限自动机、词法分析
猜你喜欢

PAT 1157:校庆

Permission model DAC ACL RBAC ABAC

2021-03-11 COMP9021第八节课笔记

About the iframe anchor, the anchor is offset up and down, and the anchor has page display problems Srcdoc problem of iframe

New technology practice, encapsulating the permission application library step by step with the activity results API

2022茶艺师(中级)上岗证题库及在线模拟考试

JUC个人简单笔记

MAYA重新拓布

Longhorn installation and use

OpenCV to realize the basic transformation of image
随机推荐
13 -- remove invalid parentheses
问题4 — DatePicker日期选择器,2个日期选择器(开始、结束日期)的禁用
2021-03-16 COMP9021第九节课笔记
Three categories of financial assets under the new standards: AMC, fvoci and FVTPL
ZUCC_编译语言原理与编译_实验04 语言与文法
Building a static website with eleventy
Win10 cloud, add Vietnamese
ZUCC_ Principles of compiling language and compilation_ Experiment 03 getting started with compiler
How to replace the web player easyplayerproactivex Key in OCX?
ZUCC_ Principles of compiling language and compilation_ Experiment 05 regular expression, finite automata, lexical analysis
貸款五級分類
Use of swift basic closure /block (source code)
Swift extension chainlayout (UI chain layout) (source code)
12-- merge two ordered linked lists
Tencent cloud ASR product PHP realizes real-time voice authentication request
[real estate opening online house selection, WiFi coverage temporary network] 500 people are connected to WiFi at the same time
Use cpulimit to free up your CPU
Promise的使用场景
Easydss anonymous live channel data volume instability optimization scheme sharing
李白最经典的20首诗排行榜