当前位置:网站首页>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赋值给当前数据,最后返回传入的对象即可(中间的赋值利用了引用数据类型的特点)。打印效果如下:

边栏推荐
- SQL intra statement operation
- Promise的使用场景
- Blue screen error UNMOUNTABLE boot volume of the solution
- Question bank and simulation examination for operation certificate of refrigeration and air conditioning equipment in 2022
- The article takes you to understand the security of Windows operating system and protect your computer from infringement
- Live broadcast appointment: growth of Mengxin Product Manager
- ZUCC_编译语言原理与编译_实验08 语法分析 LR 分析
- 2021-03-11 COMP9021第八节课笔记
- 05 Ubuntu installing mysql8
- How to implement approval function in Tekton
猜你喜欢

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

5 minutes, excellent customer service chat handling skills

ZUCC_编译语言原理与编译_实验01 语言分析与简介

ZUCC_ Principles of compiling language and compilation_ Experiment 03 getting started with compiler

ZUCC_ Principles of compiling language and compilation_ Experiment 05 regular expression, finite automata, lexical analysis

A preliminary study of IO model

新技术实战,一步步用Activity Results API封装权限申请库

The article takes you to understand the security of Windows operating system and protect your computer from infringement

Markdown 实现文内链接跳转

2021-03-09 COMP9021第七节课笔记
随机推荐
QPS, TPS, concurrent users, throughput relationship
Detailed explanation of etcd backup and recovery principle and actual record of stepping on the pit
"Adobe international certification" Photoshop software, about drawing tutorial?
Easydss anonymous live channel data volume instability optimization scheme sharing
jwt(json web token)
问题4 — DatePicker日期选择器,2个日期选择器(开始、结束日期)的禁用
Understanding of the concept of "quality"
Opencv get (propid) common values
软件过程与项目管理期末复习与重点
io模型初探
New technology practice, encapsulating the permission application library step by step with the activity results API
The JS macro of WPS implements the separation method of picture text in the same paragraph
Tencent conference API - get rest API & webhook application docking information
ZUCC_ Principles of compiling language and compilation_ Big job
Permission model DAC ACL RBAC ABAC
App Startup
小样本故障诊断 - 注意力机制代码 - BiGRU代码解析实现
5分钟,客服聊天处理技巧,炉火纯青
一文带你了解Windows操作系统安全,保护自己的电脑不受侵害
[graduation season] Hello stranger, this is a pink letter