给一个多层级的数组,写一个函数返回路径是否存在;对象{a: {b: {c: ‘d’}}} 路径 ‘a.b.c’ 返回true

    1. function hasProperty (obj, keys) {
    2. let res;
    3. try {
    4. keys.split('.').reduce((item, key) => {
    5. if (item.hasOwnProperty(key)) {
    6. if (item[key] && typeof item[key] === 'object') {
    7. return item[key];
    8. }
    9. // return true;
    10. return res = true;
    11. }
    12. return res = false;
    13. }, obj);
    14. } catch (e) {
    15. console.log(e);
    16. return res = false;
    17. }
    18. return res;
    19. }