const source = [ null, { id: 4, name: 'test1' }, { id: {}, name: 'ssdf' }, "test", { id: () => {}, name: 'sf' }, { id: '6', name: 'test3' }, { id: 6, name: 'test4' }, { id: 7, name: 'test7' }, { id: 2, name: 'test2' }, { name: 'sf' }, {}, () => {}]
function filterSort() { // 写下你的代码 }filterSort(source)// 输出结果如下['test2', 'test1', 'test4', 'test7']
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
const source = [
null,
{ id: 4, name: 'test1' },
{ id: {}, name: 'ssdf' },
"test",
{ id: () => { }, name: 'sf' },
{ id: '6', name: 'test3' },
{ id: 6, name: 'test4' },
{ id: 7, name: 'test7' },
{ id: 2, name: 'test2' },
{ name: 'sf' },
{},
() => { }
]
function filterSort(source) {
// 写下你的代码
source=source.filter(item=>item?item.id?typeof item.id==='number':false:false)
let obj={}
for(let i of source){
obj[i.id]=i.name
}
return Object.values(obj)
// var a= source.filter((item)=>item instanceof Object).filter(item=>item["id"]).filter(item=> typeof item.id==Number)
// console.log(a);
}
let res= filterSort(source)
console.log(res)
// 输出结果如下
// ['test2', 'test1', 'test4', 'test7']
</script>
</body>
</html>