Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including get-obj-deep-val with all npm packages installed. Try it out:

var getObjDeepVal = require("get-obj-deep-val")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

get-obj-deep-val v1.0.0

获取对象属性值,不论层级

获取一个对象不论多少层级的属性值,如有多个只取一个,不存在则返回undefined

案例:

const getObjDeepVal = require('get-obj-deep-val')
let x = { a: { b: { c: { d: 1 } } } };
Object.prototype.getObjVal = function(key) {
 return getObjDeepVal(this, key)
}
console.log(x.getObjVal('a')) // { b: { c: { d: 1 } } }
console.log(getObjDeepVal(x, 'a')) // { b: { c: { d: 1 } } } 
console.log(x.getObjVal('c')) // { d: 1 }
console.log(x.getObjVal('d')) // 1
console.log(x.getObjVal('g')) // undefined

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free