LLwanran / front_end_studying

前端知识要点
https://llwanran.github.io/front_end_studying/
2 stars 1 forks source link

请把俩个数组 [X1, X2, X3, Y1, Y2, Y3, Z1, Z2, Z3] 和 [X, Y, Z],合并为 [X1, X2, X3, X, Y1, Y2, Y3, Y, Z1, Z2, Z3, Z](北京今日头条1面) #33

Open LLwanran opened 5 years ago

LLwanran commented 5 years ago

该题的实际应用,譬如头条新闻列表插入广告,不光是数组,对象依然有这种需求,实际场景会更复杂

let arr1 = ['X1', 'X2', 'X3', 'Y1', 'Y2', 'Y3', 'Z1', 'Z2', 'Z3'];
let arr2 = ['X', 'Y', 'Z'].map((item) => {
  return item + 4;
});

let arr3 = [...arr1, ...arr2].sort().map((item) => {
  if (item.includes('4')) {
    return item.split('')[0];
  }
  return item;
})