FE-Interview icon indicating copy to clipboard operation
FE-Interview copied to clipboard

请写一个函数,输出出多级嵌套结构的 Object 的所有 key 值

Open lgwebdream opened this issue 5 years ago • 11 comments

var obj = {
  a: "12",
  b: "23",
  first: {
    c: "34",
    d: "45",
    second: { 3: "56", f: "67", three: { g: "78", h: "89", i: "90" } },
  },
};
// => [a,b,c,d,e,f,g,h,i]

lgwebdream avatar Jul 06 '20 15:07 lgwebdream

扫描下方二维码,获取答案以及详细解析,同时可解锁800+道前端面试题。

lgwebdream avatar Jul 06 '20 15:07 lgwebdream

var obj = {
  a: '12',
  b: '23',
  first: {
    c: '34',
    d: '45',
    second: { 3: '56', f: '67', three: { g: '78', h: '89', i: '90' }}
  }
}
function getObjAllKeys(obj, res = []) {
  for (const key in obj) {
    res.push(key)
    if (typeof obj[key] === 'object') {
      getObjAllKeys(obj[key], res)
    }
  }
  return res
}
getObjAllKeys(obj)

523451928 avatar Jul 16 '20 09:07 523451928

var obj = {
	a: "12",
	b: "23",
	first: {
		c: "34",
		d: "45",
		second: { 3: "56", f: "67", three: { g: "78", h: "89", i: "90" } },
	},
};
// => [a,b,c,d,e,f,g,h,i]
function getObjAllKeys(obj, res = []){
	if(typeof obj !== 'object' || obj === null) return [];
	for (const key in obj) {
		if (obj.hasOwnProperty(key)) {
			res.push(key);
			if(obj[key] && typeof obj[key] === 'object') {
				getObjAllKeys(obj[key], res);
			}
		}
	}
	return res;
}

GolderBrother avatar Aug 30 '20 09:08 GolderBrother

var obj = {
    a: "12",
    b: "23",
    first: {
        c: "34",
        d: "45",
        second: { 3: "56", f: "67", three: { g: "78", h: "89", i: "90" } },
    },
};
// => [a,b,c,d,e,f,g,h,i]
function getAllKeys(obj = {}, res = []) {
    if (typeof obj !== 'object' || obj === null) return [];
    for (const key in obj) {
        if (obj.hasOwnProperty(key)) {
            if (obj[key] && typeof obj[key] === 'object') {
                return getAllKeys(obj[key], res);
            }
            res.push(key);
        }
    }
    return res;
}
console.log(getAllKeys(obj))

GolderBrother avatar Aug 31 '20 14:08 GolderBrother

const obj = {
  a: "12",
  b: "23",
  first: {
    c: "34",
    d: "45",
    second: { e: "56", f: "67", three: { g: "78", h: "89", i: "90" } },
  },
}

const getAllKeys = (obj) =>
  Object.keys(obj)
    .map((key) => {
      if (Object.prototype.toString.call(obj[key]) != "[object Object]") return key
      else return getAllKeys(obj[key])
    })
    .flat(1)

console.log(getAllKeys(obj))

HW2821 avatar Sep 13 '21 05:09 HW2821

var obj = {
  a: "12",
  b: "23",
  first: {
    c: "34",
    d: "45",
    second: { e: "56", f: "67", three: { g: "78", h: "89", i: "90" } },
  },
};

function getAllKey(obj) {
  let result = [];
  for (const key in obj) {
    if (!isObj(obj[key])) {
      result = result.concat(key);
    } else {
     result = result.concat(getAllKey(obj[key]));
    }
  }
  return result;
}

function isObj(obj) {
  return Object.prototype.toString.call(obj) === '[object Object]'
}

Neisun avatar Jan 02 '22 08:01 Neisun

const main = (obj, res = []) => {
  for (const key in obj) {
    if (typeof obj[key] === 'object') {
      main(obj[key], res)
    } else {
      res.push(key)
    }
  }
  return res
}

DaphnisLi avatar May 22 '23 15:05 DaphnisLi

var objData = {
  a: "12",
  b: "23",
  first: {
    c: "34",
    d: "45",
    second: { 3: "56", f: "67", three: { g: "78", h: "89", i: "90" } },
  },
};
// => [a,b,c,d,e,f,g,h,i]
function getObjectAllKey(obj, keys = []) {
  for (const key in obj) {
    if (obj[key] instanceof Object) {
      getObjectAllKey(obj[key], keys);
    } else {
      keys.push(key);
    }
  }
  return keys;
}
console.log("getAllKey: ", getObjectAllKey(objData));

cfb22 avatar Sep 03 '23 01:09 cfb22



const printObject = (obj, cache = []) => {
  const keys = Object.keys(obj);
  keys.forEach(key => {
    if (typeof obj[key] === "object") {
      printObject(obj[key], cache);
    } else {
      cache.push(key);
    }
  });
  return cache;
};

const printObjectDemo = {
  a: "12",
  b: "23",
  first: {
    c: "34",
    d: "45",
    second: { 3: "56", f: "67", three: { g: "78", h: "89", i: "90" } },
  },
};

const obj = printObject(printObjectDemo);
console.log("obj", obj);

gaohan1994 avatar Apr 28 '24 08:04 gaohan1994