Nodejs笔试题疑问,今天遇到的 node.js 的笔试题,我在解这道题的时候,有个问题我不太明白,感觉自己傻 X 了
Nodejs笔试题疑问,今天遇到的 node.js 的笔试题,我在解这道题的时候,有个问题我不太明白,感觉自己傻 X 了
function checkLegalStr(str=’’){
return (str !== ‘’ && str !== ‘-’ && str !== ‘N/A’)
}
function handleObject(obj={}){
for(let key in obj){
if(typeof obj[key] === ‘object’){
handleObject(obj[key]);
}else if(Array.isArray(obj[key])){
obj[key] = obj[key].filter(item=>{return checkLegalStr(item);});
}else{
if(!checkLegalStr(obj[key])){
delete obj[key];
}
}
}
}
const obj = {
name: { first: ‘Robert’, middle: ‘’, last: ‘Smith’ },
age: 25,
DOB: ‘-’,
hobbies: [ ‘running’, ‘coding’, ‘-’ ],
education: { highschool: ‘N/A’, college: ‘Yale’ }
};
console.log(obj);
handleObject(obj);
console.log(obj);
为啥这个结果 obj.hobbies 会有一个空值<1 empty item>
以下是前后输出
{
name: { first: 'Robert', middle: '', last: 'Smith' },
age: 25,
DOB: '-',
hobbies: [ 'running', 'coding', '-' ],
education: { highschool: 'N/A', college: 'Yale' }
}
{
name: { first: 'Robert', last: 'Smith' },
age: 25,
hobbies: [ 'running', 'coding', <1 empty item> ],
education: { college: 'Yale' }
}
实在是奇怪,我感觉我好水啊,有知道的大哥吗