How JavaScript uses the getPropByPath function
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!