题解 | #属性遍历#
属性遍历
https://www.nowcoder.com/practice/0158a4f165154f2eaf27d1907aa55e57
function iterate(obj) {
let arr=[]
Object.keys(obj).forEach(item=>{
arr.push(item+": "+obj[item])
})
return arr
}
属性遍历
https://www.nowcoder.com/practice/0158a4f165154f2eaf27d1907aa55e57
function iterate(obj) {
let arr=[]
Object.keys(obj).forEach(item=>{
arr.push(item+": "+obj[item])
})
return arr
}
相关推荐
查看1道真题和解析
球球与墩墩:这不是前端常考的对象扁平化吗,面试官像是前端出来的
const flattern = (obj) => {
const res = {};
const dfs = (curr, path) => {
if(typeof curr === 'object' && curr !== null) {
const isArray = Array.isArray(curr);
for(let key in curr) {
const newPath = path ? isArray ? `${path}[${key}]` : `${path}.${key}` : key;
dfs(curr[key], newPath);
}
} else {
res[path] = curr
}
}
dfs(obj);
return res;
}
查看3道真题和解析