_.get
Gets the value at path of object.Note: If provided path does not exists inside the object js will generate error.
- // Lodash
- var object = { a: [{ b: { c: 3 } }] };
- var result = _.get(object, 'a[0].b.c', 1);
- console.log(result);
- // output: 3
- // Native (ES6 - IE not supported)
- var object = { a: [{ b: { c: 3 } }] };
- var { a: [{ b: { c: result2 = 1 } }] } = object;
- console.log(result2);
- // output: 3
- // Native
- const get = (obj, path, defaultValue = null) =>
- String.prototype.split.call(path, /[,[\].]+?/)
- .filter(Boolean)
- .reduce((a, c) => (Object.hasOwnProperty.call(a,c) ? a[c] : defaultValue), obj)
- var object = { a: [{ b: { c: 3 } }] };
- var result = get(object, 'a[0].b.c', 1);
- // output: 3
Browser Support for Object destructing
49.0 ✔ | 14.0 ✔ | 41.0 ✔ | ✖ | 41.0 ✔ | 8.0 ✔ |
当前内容版权归 you-dont-need 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 you-dont-need .