携程笔试第二题输入到底是一行还是很多行啊?我不知道为什么我的本地调试是对的,但是交上去什么都错
气死我了,做了45分钟这题
let str=[null, 2, "test", undefined, {
"type": "product",
"content": "product1"
}, {
"type": "product",
"content": "product2"
}, {
"type": "tag",
"content": "tag1"
}, {
"type": "product",
"content": "product3"
}, {
"type": "tag",
"content": "tag2"
}];
function groupList(list) {
for(let i=list.length-1;i>=0;i--){
if(list[i]==null||typeof list[i]!="object"){
list.splice(i,1);
}
}
let types=[];
for(let i=list.length-1;i>=0;i--){
types.push(list[i].type);
}
types=[...new Set(types)]
let product=[],tag=[],res=[];
for(let j=0;j<types.length;j++){
let newType=[];
for(let i=list.length-1;i>=0;i--){
if(list[i].type==types[j]){
newType.unshift(list[i].content);
}
}
res.push(newType);
}
let res2=[];
for(let j=types.length-1;j>=0;j--){
let tmp={type:types[j],content:res[j]}
res2.push(tmp);
}
return res2;
}
var data =str;
var result = groupList(data);
console.log(JSON.stringify(result));
考完后和大家讨论,真的气死我了,只能说贵公司出题测试样例真的牛
好气啊啊啊啊啊啊