Comments (298)
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
})
from daily-interview-question.
我想问下,这题想考的是哪方面的知识?
from daily-interview-question.
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)
from daily-interview-question.
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
}
})```
from daily-interview-question.
其实解法很简单的
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)
)
)
);
from daily-interview-question.
看一下我这个可以嘛
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)
from daily-interview-question.
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);
from daily-interview-question.
这是要考啥知识点?
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] ]
from daily-interview-question.
如果只是单纯解这道题的话,我这样做:
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"]
等等也没问题啦
from daily-interview-question.
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个参考点
- arr1和arr2本身就是有序的,A1的index和A的index成2倍关系
- 可以使用Array的toString方法将数组拍平
from daily-interview-question.
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映射改善排序的方法进行的处理,不过对数组进行了多次处理,感觉方法不太好
from daily-interview-question.
from daily-interview-question.
我想问下,这题想考的是哪方面的知识?
假设有一种情况,让你在一个列表中插入一个广告,不光是数组,对象依然有这种需求,这道题其实就是平常经常需要用到的一个小功能。
from daily-interview-question.
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入手,不依赖数组下标,通用性更强
from daily-interview-question.
// 两个数组合并成一个数组
let arr1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2']
let arr2 = ['A', 'B', 'C', 'D']
console.log([...arr1, ...arr2].sort()) // 原理 sort() 默认排序顺序是根据字符串Unicode码点。
from daily-interview-question.
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插入,因为插入位置是固定的。
from daily-interview-question.
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);
from daily-interview-question.
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)
from daily-interview-question.
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], [])
from daily-interview-question.
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 方法
from daily-interview-question.
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
})
from daily-interview-question.
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;
}
from daily-interview-question.
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码进行排序
from daily-interview-question.
你们别打啦别打啦。
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]));
康老师牛的不行
from daily-interview-question.
//start:初次插入位置
//space:插入位置间隔
function concatArray(start,space,arrOne,arrTwo){
arrTwo.forEach((ele,index)=>{
arrOne.splice(start,0,ele);
start+=space;
})
}
//在github的第一次发言 emoji emoji
from daily-interview-question.
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))
from daily-interview-question.
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()
借鉴了前面各位大神的。
from daily-interview-question.
不知道考点在哪儿,最简单的方法写了下:
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);
}, []);
楼上的解法不错,赞一个。
from daily-interview-question.
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]));
from daily-interview-question.
想到两个方式;
1:
let a1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'];
let a2 = ['A', 'B', 'C', 'D'];
a2.map((item, idx)=>{ a1.splice(2 + idx*3, 0, item); })
console.log(a1);
2:
let a1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'];
let a2 = ['A', 'B', 'C', 'D'];
let r = [];
a1.forEach((item, idx, arr)=>{
if((idx+1)%2 === 0) {
r.push(item);
r.push(a2[(idx-1)/2]);
}else{
r.push(item);
}
})
console.log(r)
愚见,个人两个方式的**一致,实现方式不同
from daily-interview-question.
let a1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2']
let a2 = ['A', 'B', 'C', 'D']
console.log([...a1,...a2].sort((a,b)=> (a+':')>(b+':')?1:-1))
from daily-interview-question.
let arr1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2']
let arr2 = ['A', 'B', 'C', 'D'];
let arr = arr1.concat(arr2);
arr.sort((a,b)=>{
return a.charCodeAt(0) - b.charCodeAt(0)
})
from daily-interview-question.
const key = 3;
const arr1 = ['A1', 'A2', 'B1', 'B2','D1', 'D2', 'C1', 'C2'];
const arr2 = ['A', 'B', 'C', 'D'].map(item => item + key);
let result = [].concat(arr1, arr2);
result.sort();
result = result.map(item => item.replace(key, ''));
console.log(result);
利用MDN上对Array.sort()的解释,默认的排序方法会将数组元素转换为字符串,然后比较字符串中字符的UTF-16编码顺序来进行排序。
from daily-interview-question.
const a1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2']
const a2 = ['A', 'B', 'C', 'D']
const arr = [...a1, ...a2]
arr.sort((a, b) => a.charCodeAt(0) - b.charCodeAt(0))
console.log(arr)
from daily-interview-question.
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);
这样是否可以呢?
from daily-interview-question.
其实解法很简单的
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 解法是对的,就是你这个函数能不能换行啊,这样长看着多不舒服
from daily-interview-question.
题目测试用例有点少规则太模糊了。。
写一个用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);
from daily-interview-question.
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"]
from daily-interview-question.
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"]
from daily-interview-question.
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)
from daily-interview-question.
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);
from daily-interview-question.
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'])
);
from daily-interview-question.
//利用字符串的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])
from daily-interview-question.
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]
},[])
from daily-interview-question.
// 思路,对字符串每个位置设置权重(重的排后面), 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))
from daily-interview-question.
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);
from daily-interview-question.
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);
from daily-interview-question.
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"]
from daily-interview-question.
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)
from daily-interview-question.
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))
from daily-interview-question.
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'])
from daily-interview-question.
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;
}
from daily-interview-question.
/单纯解题的写法/
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);
from daily-interview-question.
我觉得插入排序也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);
from daily-interview-question.
你们别打啦别打啦。
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]));
from daily-interview-question.
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('--')
from daily-interview-question.
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
}
})
from daily-interview-question.
//我短,我先来
const a =['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'];
(''+a).replace(/(.)2/g,"$12,$1").split(/,/)
from daily-interview-question.
哈哈哈,我想到了两种方法:
var arr1=['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2']; var arr2=['A', 'B', 'C', 'D']; var arr=[];
法1:(不要打我)
arr1.splice(2,0,arr2[0]); arr1.splice(5,0,arr2[1]); arr1.splice(8,0,arr2[2]); arr1.splice(11,0,arr2[3]); arr=arr1; console.log(arr);
法2:
let n=0; for(let t=0;t<4;t++){ for(let i=2*n+1;i<8;i++){ arr1.splice(i+1+n,0,arr2[n]); arr=arr1; n++; break; } }
from daily-interview-question.
const arr1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'];
const arr2 = ['A', 'B', 'C', 'D'];
const map = {};
let result = [];
arr1.forEach(item => {
let char = item.charAt(0);
if (map[char]) {
map[char].push(item);
} else {
map[char] = [].concat(item);
}
});
for (let key in map) {
if (arr2.includes(key)) {
result = result.concat(map[key], key);
}
}
console.log(result)
from daily-interview-question.
it
大哥厉害,向你学习
from daily-interview-question.
如果有其他规则又是另一个故事了
let a = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'], b = ['A', 'B', 'C', 'D']; const c = b.reduce( (all, cur) => all.concat(a.splice(0, 2), cur), [] ); console.log(c);
from daily-interview-question.
const arr1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2']
const arr2 = ['A', 'B', 'C', 'D']
const arr = new Array(arr2.length)
const arrSum = [...arr1, ...arr2]
for(let i = 0; i < arr2.length; i ++){
arr[i] = new Array()
}
arr2.map(function(item2,index2){
arrSum.forEach(function(item){
if(item.charAt() == item2){
arr[index2].push(item)
}
})
})
arr.flat()
from daily-interview-question.
懒得写啦,arr2遍历后面加+3,concat,再去掉3,哈哈哈哈哈
from daily-interview-question.
var arr = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'];
var arr2 = ['A', 'B', 'C', 'D'];
arr2 = arr2.map(res => {
return res + '3'
})
console.log(arr2.concat(arr).sort().map(item => {
if (item.indexOf('3') > -1) {
return item.split('')[0]
}
return item
}))
from daily-interview-question.
var a = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2']
var b = ['A', 'B', 'C', 'D']
for (var i = 0; i < b.length; i++) {
var c = [];
for (var j = 0; j < a.length; j++) {
j % 2 == 1 ? c.push(j + i + 1) : ''
}
a.splice(c[i], 0, b[i])
}
console.log(a) //[ 'A1', 'A2', 'A', 'B1', 'B2', 'B', 'C1', 'C2', 'C', 'D1', 'D2', 'D' ]
from daily-interview-question.
首先想到这样😂
let a=["A1","A2","B1","B2","C1","C2","D1","D2"],
B=["A","B","C","D"];
for(let i=B.length; i > 0; i--) {
a.splice(i*2,0,B[i-1]);
}
console.log(a)
输出:
["A1", "A2", "A", "B1", "B2", "B", "C1", "C2", "C", "D1", "D2", "D"]
from daily-interview-question.
见有用sort排序的 建议sort排序带字母的不要点赞了 如果是比较长的话 sort排序是不准的
例:["a1080","a20","a1"].sort()得到的顺序是["a1", "a1080", "a20"]
from daily-interview-question.
其实解法很简单的
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) ) ) );
这个答案无疑是最正确的,应该也体现出了最终题目的考点,
- 第一个条件v2.codePointAt(0) - v1.codePointAt(0) 保证了所有已A开头的字符串会放在最前边,然后依次是B和C。
- 第二个条件v1.length - v2.length保证A会被放在A1和A2之后。
- 第三个条件v2.codePointAt(1) - v1.codePointAt(1)保证了A1会被放在A2前边。
from daily-interview-question.
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插入,因为插入位置是固定的。
arr1 里面多一个A4 你这就错了
from daily-interview-question.
let arr1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2']
let arr2 = ['A', 'B', 'C', 'D'];
let arr = arr1.concat(arr2).sort((a,b)=>{
if(a.charCodeAt(0)!==b.charCodeAt(0)){
return a.charCodeAt(0)-b.charCodeAt(0);
}else{
if(a.charCodeAt(1)===''){
return -1;
}else if(b.charCodeAt(1)===''){
return 1;
}else{
return a.charCodeAt(1)-b.charCodeAt(1);
}
}
});
from daily-interview-question.
let combieArr = ( a,b ) => {
let arr = [],index = 0
for (var i = 0; i < a.length;) {
if(!b[index]) {
arr = arr.concat(a.slice(i))
break
}
if (b[index].charCodeAt(0) === a[i].charCodeAt(0)) {
arr.push(a[i])
i++
}else {
arr.push(b[index])
index++
}
}
if(b[index]) {
arr = arr.concat(b.slice(index))
}
return arr
}
from daily-interview-question.
let arr=['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'],
arr2=['A', 'B', 'C', 'D'];
arr.concat(arr2).sort((a,b)=>(a+b>b+a)-.5)
from daily-interview-question.
const arr1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'];
const arr2 = ['A', 'B', 'C', 'D'];
let ret = [];
arr1.forEach((item, index) => {
ret.push(item);
if (index % 2 === 1) {
ret.push(arr2[Math.floor(index / 2)]);
}
});
from daily-interview-question.
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], [])
牛批。心酱
from daily-interview-question.
let a = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'];
let b = ['A', 'B', 'C', 'D'];
function myConcat(a, b) {
let result = [...a];
b.forEach((item, index) => {
result.splice(3 * index + 2, 0, item);
})
return result;
}
console.log(myConcat(a,b));
from daily-interview-question.
定义排序规则,先比较第一个字符,小的放前面,在比较第二个,NaN 设置恒大
const NaNtoNumber = (num) => isNaN(num) ? 9999 : num;
[...a1, ...a2].sort((a,b)=> {
if(a.charCodeAt(0)>b.charCodeAt(0)){
return 1
}else if(a.charCodeAt(0)===b.charCodeAt(0)){
if(NaNtoNumber(a.charCodeAt(1)) >NaNtoNumber(b.charCodeAt(1))) return 1
return -1
}else return -1
})
from daily-interview-question.
比较简单的方法,仅针对题目的数据格式
var a1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2']
var a2 = ['A', 'B', 'C', 'D']
a2.map((v,i)=>{a1.splice(i*2+2+i,0,v)})
console.log(a1)
from daily-interview-question.
const a = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'];
const b = ['A', 'B', 'C', 'D'];
let r = [];
b.forEach(item=>{
const f = a.filter(ite=>{
return ite.indexOf(item)>-1
})
r = r.concat(f)
r.push(item)
})
from daily-interview-question.
见有用sort排序的 建议sort排序带字母的不要点赞了 如果是比较长的话 sort排序是不准的
例:["a1080","a20","a1"].sort()得到的顺序是["a1", "a1080", "a20"]
但是那些就是利用sort()的排序规则。不传回调的情况下,把数组的值都转成了字符串。按照字符串字符utf-16编码排序。先比较首位,相同比较第二位,以此类推。你的结果不也符合这个规则么。。
from daily-interview-question.
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映射改善排序的方法进行的处理,不过对数组进行了多次处理,感觉方法不太好
为什么不这样呢?
var c = a.concat(b).sort(function(a, b) { return +(a[0] > b[0]) || +(a[0] === b[0]) - 1; }); console.log(c)
from daily-interview-question.
const arr1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2']
const arr2 = ['A', 'B', 'C', 'D'];
let arr3 = [...arr1,...arr2];
function go(arr) {
for(let i = 0;i<arr.length;i++){
for(let j =i;j<arr.length;j++){
let obj1 = {
head:arr[i].substring(0,1),
end: arr[i].substring(1) || Infinity
}
let obj2 = {
head:arr[j].substring(0,1),
end: arr[j].substring(1) || Infinity
}
if(obj1.head === obj2.head){
if(obj1.end > obj2.end){
let num = arr[i];
arr[i] = arr[j];
arr[j] = num;
}
}else{
if(obj1.head > obj2.head){
let num = arr[i]
arr[i] = arr[j];
arr[j] = num;
}
}
}
}
return arr;
}
console.log(go(arr3))
from daily-interview-question.
其实解法很简单的
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) ) ) );
大佬膜拜
from daily-interview-question.
var a = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2']
var b = ['A', 'B', 'C', 'D']
// ['A1', 'A2', 'A', 'B1', 'B2', 'B', 'C1', 'C2', 'C', 'D1', 'D2', 'D']
var c = a.concat(b).sort((a, b) => {
a = a.charCodeAt() - (+a[1] || 0) / 10
b = b.charCodeAt() - (+b[1] || 0) / 10
return a - b
})
from daily-interview-question.
let arr1= ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'];
let arr2= ['A', 'B', 'C', 'D'];
let arr=arr1.reduce((pre,next,idx)=>{
pre.push(next)
if(idx%2){
pre.push(arr2[parseInt(idx/2,10)]);
}
return pre;
},[]);
from daily-interview-question.
`var arr1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'],
arr2 = ['A', 'B', 'C', 'D'], arr3 = [...arr1, ...arr2], arr4=[], map={};
arr3.forEach((item) => {
var code = item.charCodeAt(item);
if(map[code]) {
map[code].push(item)
} else {
map[code] = [item]
}
})
Object.values(map).forEach((item) => arr4.push(item.sort((a,b) => a>b)))
console.log(arr4.flat())`
from daily-interview-question.
['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'].concat( ['A', 'B', 'C', 'D'].map(a=>a+3)).sort().map(a=>a.replace('3',''))
from daily-interview-question.
没深入进行逻辑优化的代码:
const a1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'];
const a2 = ['A', 'B', 'C', 'D'];
const a3 = [...a1, ...a2].sort((a, b)=> {
if (a.length !== b.length) {
for (let i = 0; i < a.length; i += 1) {
if (i > b.length) {
return -1;
}
if (a.charAt(i) !== b.charAt(i)) {
return a.charAt(i) < b.charAt(i) ? -1 : 1;
}
}
return 1;
}
return a > b ? 1 : -1;
});
from daily-interview-question.
function merge (a, b) {
let res = []
for (let i = 0; i < b.length; i++) {
res.push(a[2 * i], a[2 * i + 1])
res.push(b[i])
}
return res
}
from daily-interview-question.
var arr1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2']
var arr2 = ['A', 'B', 'C', 'D']
for(var i = 0;i<arr2.length;i++){
arr1.splice(i*3+2,0,arr2[i])
}
console.log("arr1",arr1)
from daily-interview-question.
const arr1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'];
const arr2 = ['A', 'B', 'C', 'D'];
const arr = [...arr1, ...arr2];
const newArr = arr.sort((pre, next) => {
return pre.charCodeAt() - next.charCodeAt();
});
console.log(arr);
这个方法在我的node环境(v10.12.0)排序是有问题的,但是浏览器环境是好的,不知道为什么?难道是因为之前v8改写了sort的底层排序这个原因造成的?
from daily-interview-question.
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 })
6666666这个方法最好了 简洁明了
from daily-interview-question.
var a = ["A1", "A2", "B1", "B2", "C1", "C2", "D1", "D2"];
var b = ["A", "B", "C", "D"];
for (let i = 0; i < b.length; i++) {
a.splice(i * 2 + 2 + i, 0, b[i]);
}
console.log(a);
from daily-interview-question.
const a1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2'];
const a2 = ['A', 'B', 'C', 'D'];
a1.concat(a2)concat(['A', 'B', 'C', 'D']).sort().sort(
(a, b) => (
a.charAt(0) === b.charAt(0) && a.length > b.length ? -1 : 0
)
);
from daily-interview-question.
arr1 push两个元素到arr3 然后arr2 push 1个元素到arr3。
let arr1= ["A1", "A2", "B1", "B2", "C1", "C2", "D1", "D2"];
let arr2 =["A", "B", "C", "D"];
let arr3 = [];
let i =0;
for (const [index,item] of arr1.entries()) {
if (index%2===0 && index!==0) {
arr3.push(arr2[i++]);
}
arr3.push(item);
}
console.log(arr3)
from daily-interview-question.
找规律得出arr2需要插入的位置
let arr1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2']
let arr2 = ['A', 'B', 'C', 'D']
let x = 0 // arr2的下标
let y = 2 // 第一个y需要插入的位置
for(let i = 0; i < arr2.length; i ++){
arr1.splice(y, 0, arr2[x])
y += 3
x++
}
console.log(arr1)
from daily-interview-question.
不会
from daily-interview-question.
codePointAt
这个才是最通用的
from daily-interview-question.
unionArray = (a1, a2) =>
[...a1, ...a2].sort((a, b) => {
if (a.includes(b)) {
return -1;
}
if (b.includes(a)) { return 1; }
if (a < b) { return -1; }
return 1;
})
from daily-interview-question.
let arr1 = ['A1', 'A2', 'B1', 'B2', 'C1', 'C2', 'D1', 'D2']
let arr2 = ['A', 'B', 'C', 'D']
let newArr = [];
let a = 0;
arr1.forEach((i, index) => {
newArr.push(i)
if (index !== 0 && index % 2) {
newArr.push(arr2[a])
a++;
}
})
console.log(newArr)
from daily-interview-question.
Related Issues (20)
- ``` javascript HOT 2
- 反转数字字符串 HOT 2
- es11
- 实现
- [...new Set(arr.toString().split(',').sort((a,b) => a-b ))].map(item => Number(item))
- let nums1 = [3,3,2] let nums2 = [1,2,3,3,2,2,4444] function res (nums1,nums2){ let a = [] for (const item of nums1) { for (const i in nums2) { if(item==nums2[i]){ a.push(nums2[i]) nums2.splice(i,1) break } } }; return a } console.log(res(nums1,nums2))
- splice插入
- 自己回答一波,若有错误请各位指出,互相学习!谢谢 ~
- 递归
- 第226题:TS 中逆变和协变如何理解
- objmap
- 通过协程来实现
- 这代码写的...
- 第 161 题:用最精炼的代码实现数组非零非负最小值 index HOT 2
- No description provided.
- > 就我的使用来说(Vue)key的作用是为了在数据变化时强制更新组件,以避免“原地复用”带来的副作用,官方文档也说明了**不带key性能更好**,见:[List Rendering - key](https://cn.vuejs.org/v2/guide/list.html#key)
- 解释原因
- an
- 同步/异步 > 微任务>宏任务
- Object.prototype,map
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from daily-interview-question.