函數(shù)式編程
2023-05-25 18:16:14
編寫(xiě)一個(gè)名為 myFun 的函數(shù),該函數(shù)將一個(gè)對(duì)象作為其參數(shù),并在數(shù)組中返回該對(duì)象的屬性名稱(chēng)。例如,如果它接收到 {a:1,b:3} 作為參數(shù),它應(yīng)該返回 [a, b],或者如果它接收到 {u:4, k:3, h:5},它應(yīng)該返回[你,k,h]。注意我知道 Object.Keys(object) 返回 ['a', 'b', 'c']//this function should return the name of the propertyfunction myFun(object) { object = { a: 1, b: 2, c: 3 } for (obj in object) { console.log(obj); }} myFun();//testcase : console.log(myFun({a:6})[0]) which should return [a], is it actually possible or am I asking the wrong question?
2 回答
慕絲7291255
TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個(gè)贊
要獲取對(duì)象鍵數(shù)組:
const keys = Object.keys(object);
像您描述的那樣打印它們:
console.log(`[${keys.join(',')}]`);
把它組合成一個(gè)函數(shù):
function myFun(object) {
const keys = Object.keys(object);
return `[${keys.join(',')}]`;
}
森欄
TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超5個(gè)贊
這是對(duì)象的keys
var obj = {
? ? ? ? a: 1,
? ? ? ? b: 2,
? ? ? ? c: 3
? ? ? };
? ? ??
console.log(Object.keys(obj));
添加回答
舉報(bào)
0/150
提交
取消
