Advanced-Frontend / Daily-Interview-Question

我是依扬(木易杨),公众号「高级前端进阶」作者,每天搞定一道前端大厂面试题,祝大家天天进步,一年后会看到不一样的自己。
https://muyiy.cn/question/
27.4k stars 3.29k forks source link

第 30 题:请把俩个数组 [A1, A2, B1, B2, C1, C2, D1, D2] 和 [A, B, C, D],合并为 [A1, A2, A, B1, B2, B, C1, C2, C, D1, D2, D]。 #39

Open jefferyE opened 5 years ago

jefferyE commented 5 years ago
function concatArr (arr1, arr2) {
   const arr = [...arr1];
  let currIndex = 0;
 for (let i = 0; i < arr2.length; i++) {
    const RE = new RegExp(arr2[i])
    while(currIndex < arr.length) {
      ++currIndex
      if (!RE.test(arr[currIndex])) {
         arr.splice(currIndex, 0, a2[i])
         break;
       }
     }
   }
  return arr
 }
 var a1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'] 
 var a2 = ['A', 'B', 'C', 'D']
 const arr = concatArr(a1, a2)
 console.log(a1) // ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'] 
 console.log(a2) // ['A', 'B', 'C', 'D']
 console.log(arr) // ['A1', 'A2', 'A', B1', 'B2', 'B', C1', 'C2', 'C', D1', 'D2', 'D'] 

以上是我个人想法,有更好方法的欢迎讨论

GitHdu commented 5 years ago
const arr1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2']
const arr2 = ['A', 'B', 'C', 'D']
const ret = []
let tmp = arr2[0]
let j = 0
for (let i=0;i<arr1.length;i++) {
  if (tmp === arr1[i].charAt(0)){
    ret.push(arr1[i])
  }else {
    ret.push(tmp)
    ret.push(arr1[i])
    tmp=arr2[++j]
  }
   if(i===arr1.length-1){
      ret.push(tmp)
    }
}
console.log(ret)
atheist1 commented 5 years ago
var a = ['A1','A2','B1','B2','C1','C2','D1','D2']
var b = ['A','B','C','D']
// 对需要排序的数字和位置的临时存储
var mapped = a.concat(b).map(function(el, i) {
  return { index: i, value: /\D$/.test(el) ? (el + 4) : el };
})
mapped.sort(function(a, b) {
  return +(a.value > b.value) || +(a.value === b.value) - 1;
});
var result = mapped.map(function(el){
  return a.concat(b)[el.index];
});

利用mdn对sort映射改善排序的方法进行的处理,不过对数组进行了多次处理,感觉方法不太好

JJL-SH commented 5 years ago
let a1 =  ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2']
let a2 = ['A', 'B', 'C', 'D'].map((item) => {
  return item + 3
})

let a3 = [...a1, ...a2].sort().map((item) => {
  if(item.includes('3')){
    return item.split('')[0]
  }
  return item
})
gaomin commented 5 years ago

var arr1 = ["A1", "A2", "B1", "B2", "C1", "C2", "D1", "D2"]
var arr2 = ["A", "B", "C", "D"]
var arr3 = arr1.concat(arr2);
arr3.sort().sort(function(a,b){
   if (a.charAt(0) == b.charAt(0) && a.length > b.length){
       return -1
   }

})```
ESnail commented 5 years ago

var arr1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2']; var arr2 = ['A', 'B','C', 'D'];

function fn (arr1, arr2) { let arr3 = [...arr1]; let index = -1; arr2.forEach((v, i) => { index = index + 3; arr3.splice(index, 0, v); }); return arr3; }

console.log(fn(arr1, arr2)); // [ 'A1', 'A2', 'A', 'B1', 'B2', 'B', 'C1', 'C2', 'C', 'D1', 'D2', 'D' ] // -1 + 3 = 2 // 2 + 3 = 5 // 5 + 3 = 8 // 8 + 3 = 11 // 首先想到了用concat+sort,但结果不对。后来想观察了一下,用splice插入,因为插入位置是固定的。

veaba commented 5 years ago

看一下我这个可以嘛

var a1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'];
var a2 =['A','B','C','D'];
var j=-1;
var arr=[]
for(let i=0;i<a1.length;i++){
    if(i%2 ===0){
    j++
    arr=arr.concat((a1.slice(i,i+2)).concat(a2[j]))
    }
}
console.log(arr)

—————————————————————— 截图 image

acmu commented 5 years ago

我想问下,这题想考的是哪方面的知识?

DraCod commented 5 years ago

var arrOne =["A1", "A2", "B1", "B2", "C1", "C2", "D1", "D2"]; var arrTwo = ['A', 'B', 'C', 'D'];

for (let i = 0; i < arrTwo.length; i++) { let re = new RegExp(arrTwo[i], 'g'); for (let x = arrOne.length; x > 0; x--) { if(re.test(arrOne[x])){ arrOne.splice(x+1,0,arrTwo[i]) } } } console.log(arrOne);

这样是否可以呢?

dorseysen commented 5 years ago

image

Rashomon511 commented 5 years ago
let arrA = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'];
let arrB = ['A', 'B', 'C', 'D',];
function combine(a, b) {
  while (b.length){
    let str =  b.shift();
    let indexNum = 0;
    a.forEach((item,index) => {
      if(item.indexOf(str) !== -1){
        indexNum = index
      }
    })
    a.splice(indexNum + 1, 0, str)
  }
  return a;
}
tyosssss commented 5 years ago

var arr1 = ["A1", "A2", "B1", "B2", "C1", "C2", "D1", "D2"];
var arr2 = ["A", "B", "C", "D"];

arr2.forEach((it, index) => {
  arr1.splice((index + 1) * 2 + index, 0, it);
});

console.log(arr1);
blockmood commented 5 years ago

我想问下,这题想考的是哪方面的知识?

假设有一种情况,让你在一个列表中插入一个广告,不光是数组,对象依然有这种需求,这道题其实就是平常经常需要用到的一个小功能。

Mizxinp commented 5 years ago
const arr1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'];
const arr2 = ['A', 'B', 'C', 'D']
const arr = [...arr1,...arr2]
let targetArr = [];
arr2.forEach(item => {
  arr.forEach(ele=>{
      if(ele.includes(item)){
    targetArr.push(ele)
       }
   })
}); 
console.log('targetArr',targetArr);
bigbigbo commented 5 years ago

如果只是单纯解这道题的话,我这样做:

const res = ["A", "B", "C", "D"].reduce(
  (memo, item) => {
    const tmp = [...memo].reverse();
    const idx = memo.length - tmp.findIndex(i => i.startsWith(item)) - 1;

    return [...memo.slice(0, idx + 1), item, ...memo.slice(idx + 1)];
  },
  ["A1", "A2", "B1", "B2", "C1", "C2", "D1", "D2"]
);

这样即使是["A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", "D1", "D2"]等等也没问题啦

Moriarty02 commented 5 years ago
const arr1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2']
const arr2 = ['A', 'B', 'C', 'D']
function combineArr(a1, a2) {
  const ret = a2.map((item, index) => {
    return [a1[index * 2], a1[index * 2 + 1], item]
  })
  return ret.toString().split(',')
}
console.log(combineArr(arr1, arr2))

2个参考点

  1. arr1和arr2本身就是有序的,A1的index和A的index成2倍关系
  2. 可以使用Array的toString方法将数组拍平
fengT-T commented 5 years ago
const matchIndex = str => str.match(/\d+/) || []
const getCharCode = str => str.match(/\w/)[0].charCodeAt()
const result = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2']
  .concat(['A', 'B', 'C', 'D'])
  .sort((a,b) => {
    const [[aIndex = Infinity], [bIndex = Infinity]] = [matchIndex(a), matchIndex(b)]
    const [aChar, bChar] = [getCharCode(a), getCharCode(b)]
    return aChar === bChar
      ? aIndex - bIndex
      : aChar - bChar
  })
console.log(result)

提一个新思路的版本,从修改sort入手,不依赖数组下标,通用性更强

Lueny-cn commented 5 years ago
let arr1 = ["A1", "A2", "B1", "B2", "C1", "C2", "D1", "D2"]
  , arr2 = ["A", "B", "C", "D"]

function concatArr(arr1, arr2) {
  let newArr = []

  while (arr2.length !== 0) {
    let tag2 = arr2.pop()

    newArr.unshift(tag2)

    while (arr1.length !== 0) {
      let tag1 = arr1.pop()

      if (tag1.includes(tag2)) {
        newArr.unshift(tag1)
      } else {
        arr1.push(tag1)
        break
      }
    }
  }
  return newArr
}

console.log(arr1)
console.log(arr2)
console.log(concatArr(arr1, arr2))
ghost commented 5 years ago

其实解法很简单的

let arr1 = ["A1", "A2", "B1", "B2", "C1", "C2", "D1", "D2"];
let arr2 = ["A", "B", "C", "D"];
console.log(
  [...arr1, ...arr2]
    .sort(
      (v2, v1) => (
        v2.codePointAt(0) - v1.codePointAt(0) ||
        v1.length - v2.length ||
        v2.codePointAt(1) - v1.codePointAt(1)
      )
    )
);
jjeejj commented 5 years ago

其实解法很简单的

let arr1 = ["A1", "A2", "B1", "B2", "C1", "C2", "D1", "D2"];
let arr2 = ["A", "B", "C", "D"];
console.log([...arr1, ...arr2].sort((v2, v1)=>(v2.codePointAt(0) - v1.codePointAt(0) ? v2.codePointAt(0) - v1.codePointAt(0) : (v1.length - v2.length) || v2.codePointAt(1) - v1.codePointAt(1))));

@liuliangsir 解法是对的,就是你这个函数能不能换行啊,这样长看着多不舒服

BaconZhang commented 5 years ago

const arr1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'] const arr2 = ['A', 'B', 'C', 'D'] const res = [].concat(...arr2.map(i => arr1.filter(j => j.startsWith(i)).concat(i))) console.log(res)

cb3570594 commented 5 years ago
let arrA = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'];
let arrB = ['A', 'B', 'C', 'D',];
arrA.map(item => {
  if(item == arrB[0]+2) {return [item,arrB.shift()] } 
  else{return item}
}).flat()

借鉴了前面各位大神的。

yeyan1996 commented 5 years ago
var arr1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2']
var arr2 = ['A', 'B', 'C', 'D']

const func = (arr1, arr2) => arr2.reduce((acc, cur) => [...acc, ...arr1.filter(item => item.startsWith(cur)), cur], [])
WozHuang commented 5 years ago

题目测试用例有点少规则太模糊了。。

写一个用sort的吧

const arr1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'];
const arr2 = ['A', 'B', 'C', 'D'];
const arr3 = arr1.concat(arr2);
const comp = function(a,b){
    const len = Math.max(a.length, b.length);
    for(let i = 0; i < len; i++){
        if(a.charAt(i) === "") return 1;
        if(b.charAt(i) === "") return -1;
        if(a.charAt(i) !== b.charAt(i)){
            return a.charAt(i) > b.charAt(i) ? 1:-1;
        }
    }
    return 0;
}
arr3.sort(comp);
console.log(arr3);
ghost commented 5 years ago
let a1 =  ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2']
let a2 = ['A', 'B', 'C', 'D'].map((item) => {
  return item + 3
})

let a3 = [...a1, ...a2].sort().map((item) => {
  if(item.includes('3')){
    return item.split('')[0]
  }
  return item
})
bran-nie commented 5 years ago
let a =  ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2']
let b = ['A', 'B', 'C', 'D']
let arr = a.concat(b).sort()

let tmp = '', r = []
arr.forEach((item, index, a) => {
    if (item.length === 1 && tmp === '') {
    tmp = a[0]
    } else if (item.length === 1) {
    r.push(tmp)
    tmp = item
    } else {
    r.push(item)
    }

    // 这里是将最后获取的单个值,push到最后。
    if (index === a.length -1) {
    r.push(tmp)
    }
})

console.log(r) // ["A1", "A2", "A", "B1", "B2", "B", "C1", "C2", "C", "D1", "D2", "D"]

想法是将合并后的数组sort完,将如A、B替换到A1等的后面。这样的好处是只需要一次遍历。缺点是仅仅针对题目的数据格式,即A1 和 A的字符串长度。

其实一开始想到的是下面这种替换合并后的数组。不过感觉不如新起一个数组

let a =  ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2']
let b = ['A', 'B', 'C', 'D']
let arr = a.concat(b).sort()
let tmp = ''
arr.forEach((item, index, a) => {    
    if(item.length === 1 && tmp === '') {
        tmp = a.splice(0, 1)
    } else if(item.length === 1) {
        tmp = a.splice(index, 1, ...tmp)
    } else if(index === a.length -1) {
        a.push(...tmp)
    }
})
console.log(arr) // ["A1", "A2", "A", "B1", "B2", "B", "C1", "C2", "C", "D1", "D2", "D"]
underlineMY commented 5 years ago
let arr1 = ['A1', 'A2', 'A3', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'];
let arr2 = ['A', 'B', 'C', 'D','E'];
let newArr1 = [];
newArr1 = arr2.map((item)=>{
    let arr3 = arr1.filter((value)=> value.startsWith(item));
    arr3.push(item);
    return arr3
})
console.log(newArr1.join(',').split(','));
//["A1", "A2", "A3", "A", "B1", "B2", "B", "C1", "C2", "C", "D1", "D2", "D", "E"]
zhoufeifan commented 5 years ago
  const arr1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2']
  const arr2 = ['A', 'B', 'C', 'D']
  const arr = [...arr1,...arr2]
  arr.sort((a,b)=>{
    // 用字符 : 去做默认占位,因为它 的 ASCII 码大于数字9
    if(a.length > b.length) {
      b = b.padEnd(a.length,':')
    }else {
      a = a.padEnd(b.length,':')
    }
    if(a>b) return 1
    if(a<b) return -1
    return 0
  })
 console.log(arr)
YunShengTeng commented 5 years ago
const a = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'];
const b = ['A', 'B', 'C', 'D'];
// ['A1', 'A2', 'A', 'B1', 'B2', 'B', 'C1', 'C2', 'C', 'D1', 'D2', 'D'];

for (let i = 0; i < b.length; i++) {
  const n = b[i];
  const xx = i + (2 * (i + 1));
  a.splice(xx, 0, n);
}

console.log(a);
DeyaoCai commented 5 years ago
console.log(
  function concatArr() {
    return [].concat.apply([], arguments).sort((a, b) => {
      // 如果a 小 在前 则返回 -1
      return a[0] < b[0]
        ? -1
        : a[1]
          ? 1
          : a[1] - b[1];
    });
  }(['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'], ['A', 'B', 'C', 'D'])
);
sunseekers commented 5 years ago

//利用字符串的charCodeAt 进行排序,some 只要找到一个符合条件的就不要在进行循环了 var array = ['A1', 'A2','B1', 'B2', 'C1', 'C2', 'D1', 'D2'] var array1 = ['A','B','C','D'] var result = array//重新定义一个新的数组,不影响原来的数组 var array2 = array.join('').split('') array1.some((x,index)=>{ array2.some((y,key)=>{ if(y.charCodeAt()>x.charCodeAt()){ return result.splice((key/2)+index,0,x) } }) }) result.push(array1[array1.length-1])

timtnleeProject commented 5 years ago
const a = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2']
const b = ['A', 'B', 'C', 'D']

const result = a.reduce((acc,cv,idx)=>{
  return (idx%2===1)?[...acc,cv,b[(idx-1)/2]]:[...acc,cv]
},[])
jiangjiang01 commented 5 years ago

const a1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'];
const a2 = ['A', 'B', 'C', 'D'];

const result = [...a1, ...a2].sort((a, b) => a.charCodeAt() - b.charCodeAt());
console.log(result);
// ["A1", "A2", "A", "B1", "B2", "B", "C1", "C2", "C", "D1", "D2", "D"]

// sort 方法参数为回调函数,该函数接受两个参数,表示进行比较的两个数组成员;
// 排序的规则为,如果该函数的返回值大于0,表示第一个成员排在第二个成员后面,
// 否则都是第一个成员排在第二个成员前面

String.prototype.charCodeAt 方法

DeyaoCai commented 5 years ago
// 思路,对字符串每个位置设置权重(重的排后面), 0 位最终, 1位次之,以此类推
// 根据位置上的值设置权重, 根据题意得, 为空时权重最高(10 + 26 +1),由于没有字母和数字的对比,无法判断他们排序权重,故设置 0-9 分别为i 0 - 9,字母按顺序 + 10;

const max = 26 + 10 + 1;
const map = {undefined: max, null: max, "":max};
// 添加数字索引
const nums = new Array(10).fill(0);
nums.forEach((item, index) => map[index] = index);
// 添加字符索引
const chars = `ABCDEFGHIJKLMNOPQRSTUVWXYZ`;
[].forEach.call(chars, (item, index) => map[item] = index + 10);

// 判断两字符串最长长度
function getMaxLen(a, b){return Math.max(a.length, b.length)};
// 计算字符串权重
function getComputedValue(str, len, returnValue = 0){
    for(let i = 0; i < len; i++) {returnValue = returnValue + map[str[i]] * Math.pow(max, len - i)}
    return returnValue;
}
// 根据权重排序
function sort(a, b) {
    const maxLen = getMaxLen(a, b);
    return getComputedValue(a, maxLen) - getComputedValue(b, maxLen)
}
// 
const a1 = ['A', 'A2', 'B', 'B2', 'C1', 'C2', 'D1', 'D2'];
const a2 = ['A1', 'B1', 'C', 'D'];
console.log(a1.concat(a2).sort(sort))
k-0311 commented 5 years ago

let arr1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'] let arr2 = ['A', 'B', 'C', 'D'] arr1.concat(arr2).sort((a, b) => a.charAt(0).charCodeAt() - b.charAt(0).charCodeAt()); 合并数组然后用每一项的ASCII码进行排序

chen86860 commented 5 years ago

不知道考点在哪儿,最简单的方法写了下:

const arr1 = ["A1", "A2", "B1", "B2", "C1", "C2", "D1", "D2"];
const arr2 = ["A", "B", "C", "D"];

const arr3 = arr1.reduce((prev, curr, index) => {
  if (index > 0 && index % 2 === 0) {
    return prev.concat(...arr2.splice(0, 1), curr);
  }
  return prev.concat(curr);
}, []);

楼上的解法不错,赞一个。

shizhenbin commented 5 years ago

这是要考啥知识点? const arr1 = ["A1", "A2", "B1", "B2", "C1", "C2", "D1", "D2"]; const arr2 = ["A", "B", "C", "D"]; const arr3 = [arr1[0] , arr1[1], arr2[0], arr1[2] , arr1[3], arr2[1], arr1[3] , arr1[4], arr2[2] ]

te3 commented 5 years ago
const arr1 = ["A1", "A2", "B1", "B2", "C1", "C2", "D1", "D2"];
const arr2 = ["A", "B", "C", "D"];

const result = [...arr1, ...arr2].sort((a, b) => {
  const [letterA, noA = "3"] = a.split("");
  const [letterB, noB = "3"] = b.split("");

  return letterA + noA > letterB + noB ? 1 : -1;
});

console.table(result);
flute commented 5 years ago
let a = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'];
let b = ['A', 'B', 'C', 'D'];
let result = [];

a.forEach((item, index) => {
  result.push(item)
  if (index % 2 != 0) result.push(b.shift())
})

console.log(result);
hoodHan commented 5 years ago

var a1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'] var a2 = ['A', 'B', 'C', 'D']; a1.forEach((item,index)=>{ let nowIndex= a2.indexOf(item[0]); a2.splice(nowIndex,0,item) }) console.log(a2) //["A1", "A2", "A", "B1", "B2", "B", "C1", "C2", "C", "D1", "D2", "D"]

HOTAO commented 5 years ago
const arr1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2']
const arr2 = ['A', 'B', 'C', 'D']
const arr = arr2.reduce((accumutor, currentValue) => {
  const idx = accumutor.findIndex(item => item.startsWith(currentValue) && item.endsWith(2))
  return [...accumutor.slice(0, idx + 1), currentValue, ...accumutor.slice(idx + 1)]
}, arr1)
console.log(arr)
yongboo commented 5 years ago
    let a1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'];
    let a2 = ['A', 'B', 'C', 'D'];

    function concatArr(a1, a2) {
        let result = []
        while(a1.length > 0) {
            result = result.concat(a1.splice(0, 2)).concat(a2.shift())
        }
        return result
    }

    console.log(concatArr(a1, a2))
Caitingwei commented 5 years ago
function mergeArray(a,b) {
    return a.concat(b).sort((a,b) => {
        if (a.charCodeAt() === b.charCodeAt()) { return 0 };
                return a.charCodeAt() > b.charCodeAt() ? 1 : -1;
    });
};
mergeArray(['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'],['A', 'B', 'C', 'D'])
chaijinsong commented 5 years ago
arr1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'];
arr2 = ['A', 'B', 'C', 'D'];

/** 思路:
 *    1. arr2按照字母从小到大排序
 *    2. 遍历arr2,找到arr1中有当前遍历元素的字母,根据数字从小到大排序,A放在最后
 *    3. 将上面的数组concat到新数组中,依次执行该操作
 * */

function fn(arr1, arr2) {
  let result = [];
  arr2 = arr2.sort((a,b) => a > b);
  arr2.forEach(element => {
    let tmp = arr1.filter(item => item.includes(element)).sort((a,b) => a > b);
    tmp.push(element);
    result = result.concat(tmp);
  });
  return result;
}
Xiao3255 commented 5 years ago

/单纯解题的写法/ var arrA = ["A1", "A2", "B1", "B2", "C1", "C2", "D1", "D2"]; var arrB = ["A", "B", "C", "D"]; var arrC = [...arrA, ...arrB]; arrC.sort((a, b) => { if (a < b) { if(a.length < b.length) { if(a[0] === b[0] && a.length === 1) { return 1 } } return -1 } }); console.log(arrC);

kexiaofu commented 5 years ago

我觉得插入排序也ok,不合并再排序。或许只针对这个题目有效。而且 indexof 的性能的确可能会比自己做比较运算的慢,所以仅供参考。

let a = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'], b = ['A', 'B', 'C', 'D'];

let d = [];
a.map(item => {
  if (item.indexOf(b[0]) === -1) {
    d.push(b[0]);
    b.shift()
  }
  d.push(item);
})
// 如果是数组b中特有的,就最后合并
d = d.concat(b);
lkangd commented 5 years ago

你们别打啦别打啦。

const a = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'];
const b = ['A', 'B', 'C', 'D'];

[...a, ...b].sort((a, b) => a.charCodeAt(0) - b.charCodeAt(0) || (!a[1] ? 1 : a[1] - b[1]));
JC121266 commented 5 years ago

你们别打啦别打啦。

const a = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'];
const b = ['A', 'B', 'C', 'D'];

[...a, ...b].sort((a, b) => a.charCodeAt(0) - b.charCodeAt(0) || (!a[1] ? 1 : a[1] - b[1]));

康老师牛的不行

Bert0324 commented 5 years ago
var a1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'];
var a2 = ['A', 'B', 'C', 'D'];
a1.concat(a2).sort((a,b)=>a[0] === b[0] ? a.length === b.length ? a.substr(1) - b.substr(1) : b.length - a.length : a[0].localeCompare(b[0]));
chphaeton commented 5 years ago

const arr1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'] let arr2 = ['A', 'B', 'C', 'D'] arr2 = arr2.map(e => e + e) const result = [...arr1, ...arr2].sort().join('--').replace(/([A-Z]){2}/g, $1 => $1.charAt(0)).split('--')

869717581 commented 5 years ago

1个数组就可以操作了 const arr = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'] var arr3 =[] arr.map((item)=>{ if(item.charAt(1)==='2'){ arr3.push(item) arr3.push(item.charAt(0)) }else{ item.push } })