guyuezhai / interviewSummary

some summary of interview
0 stars 0 forks source link

请把俩个数组['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'] 和['A', 'B', 'C', 'D'],合并为 ['A1', 'A2', 'A', 'B1', 'B2', 'B', 'C1', 'C2', 'C', 'D1', 'D2', 'D'] #35

Open guyuezhai opened 4 years ago

guyuezhai commented 4 years ago
let arr1=['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'];
let arr2=['A', 'B', 'C', 'D'];
let result=arr1.concat(arr2.reduce((res,cur)=>{
     res.push(cur+3)
     return res
},[])).sort().map(item=>{
    let reg=item.match(/(\w)3/)
    if(reg) return reg[1]
    return item
})
console.log('result',result)

result [ 'A1', 'A2', 'A', 'B1', 'B2', 'B', 'C1', 'C2', 'C', 'D1', 'D2', 'D' ]

x906674935 commented 4 years ago

studyDemo = () => { let arr1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2']; let arr2 = ['A', 'B', 'C', 'D']; let arr3 = arr1.concat(arr2); console.log(arr3) let res = arr3.sort((a, b) => { let aaa = a.split(""); let bbb = b.split("") if (aaa[0] > bbb[0]) { return 1; } else if (aaa[0] < bbb[0]) { return -1; } else if (aaa[0] === bbb[0] && aaa[1] && bbb[1] && aaa[1] > bbb[1]) { return 1; } else if (aaa[0] === bbb[0] && aaa[1] && bbb[1] && aaa[1] < bbb[1]) { return -1; } else if (aaa[0] === bbb[0] && !aaa[1]) { return 1; } else if (aaa[0] === bbb[0] && !bbb[1]) { return -1; } else { return 0; } }); console.log(res) }