Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How JavaScript uses the getPropByPath function

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces JavaScript how to use the getPropByPath function, the article is very detailed, has a certain reference value, interested friends must read it!

GetPropByPath: get object attributes based on string path: 'obj [0] .count'

Function getPropByPath (obj, path, strict) {let tempObj = obj; path = path.replace (/\ [(\ w+)\] / g,'.$ 1'); / / convert [0] to .0 path = path.replace (/ ^\ /,''); / / remove the beginning. Let keyArr = path.split ('.'); / / according to. Cut let I = 0; for (let len = keyArr.length; I < len-1; + + I) {if (! tempObj & &! strict) break; let key = keyArr [I]; if (key in tempObj) {tempObj = tempObj [key] } else {if (strict) {/ / enable strict mode, do not find the corresponding key value, throw error throw new Error ('please transfer a valid prop path to form itemized');} break }} return {o: tempObj, / / Raw data k: keyArr [I], / / key value v: tempObj? TempObj [keyArr [I]: the value corresponding to the null / / key value};}; this is all the content of the article "how JavaScript uses getPropByPath functions". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report