列表

详情


FED74. 属性遍历

描述

找出对象 obj 不在原型链上的属性(注意这题测试例子的冒号后面也有一个空格~)
1、返回数组,格式为 key: value
2、结果数组不要求顺序

示例1

输入:

var C = function() {this.foo = 'bar'; this.baz = 'bim';}; 
C.prototype.bop = 'bip'; 
iterate(new C());

输出:

["foo: bar", "baz: bim"]

原站题解

HTML/CSS/JavaScript 解法, 执行用时: 855ms, 内存消耗: 77832KB, 提交时间: 2020-11-23

{"css":"","js":"function iterate(obj) {\n    var ownPropertyArr = [];\n    Object.keys(obj).map(function(key){\n        if(obj.hasOwnProperty(key)){\n            ownPropertyArr.push(key + ': ' + obj[key]);\n        }\n    });\n    return ownPropertyArr;\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 859ms, 内存消耗: 77900KB, 提交时间: 2021-03-15

{"css":"","js":"function iterate(obj) {\n    let res = [];\n    for (let key in obj) {\n        if (obj.hasOwnProperty(key)) res.push(`${key}: ${obj[key]}`);\n    }\n    return res;\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 860ms, 内存消耗: 77772KB, 提交时间: 2021-07-18

{"css":"","js":"function iterate(obj) {\n   return Object.keys(obj).map( k => `${k}: ${obj[k]}`)\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 860ms, 内存消耗: 77796KB, 提交时间: 2021-07-21

{"css":"","js":"function iterate(obj) {\n    var arr=Object.keys(obj);\n    var arrs=[];\n    arr.forEach(function(item){\n        arrs.push(item+\": \"+obj[item])\n    })\n    \n    return arrs;\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 861ms, 内存消耗: 77772KB, 提交时间: 2021-04-04

{"css":"","js":"function iterate(obj) {\n    return Object.getOwnPropertyNames(obj).map(item => {\n        return item +': '+ obj[item]\n    })\n}","html":"","libs":[]}

上一题