博客
关于我
算法系列1--js数组排序
阅读量:346 次
发布时间:2019-03-03

本文共 1081 字,大约阅读时间需要 3 分钟。

两个数组合并成一个数组

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

 自己研究的解法:

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' ]

其他的解法: 

 

let result = ["A1", "A2", "B1", "B2", "C1", "C2", "D1", "D2"].concat(["A", "B", "C", "D"]).sort((a,b)=>a.charAt(0).charCodeAt()-b.charAt(0).charCodeAt());

改造下面的代码,使之输出0 - 9,写出你能想到的所有解法。

下面的代码会输出10个10 

for (var i = 0; i< 10; i++){	setTimeout(() => {		console.log(i);    }, 1000)}

 改造后,输出0-9

for (var i = 0; i < 10; i++) {  (     (j) => {      setTimeout(() => {        console.log(j);      }, 1000)    }  )(i)}
for (let i = 0; i < 10; i++) {  setTimeout(() => {    console.log(i);  }, 1000)}
for (var i = 0; i < 10; i++) {  setTimeout((i) => {    console.log(i);  }, 1000, i)}

 

 

转载地址:http://opyq.baihongyu.com/

你可能感兴趣的文章
No static resource favicon.ico.
查看>>
no such file or directory AndroidManifest.xml
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>
no1
查看>>
NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
查看>>
NOAA(美国海洋和大气管理局)气象数据获取与POI点数据获取
查看>>
NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
查看>>
node
查看>>
node exporter完整版
查看>>
node HelloWorld入门篇
查看>>
Node JS: < 一> 初识Node JS
查看>>
Node JS: < 二> Node JS例子解析
查看>>
Node Sass does not yet support your current environment: Linux 64-bit with Unsupported runtime(93)解决
查看>>
Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime(72)
查看>>
Node 裁切图片的方法
查看>>
node+express+mysql 实现登陆注册
查看>>
Node+Express连接mysql实现增删改查
查看>>
node, nvm, npm,pnpm,以前简单的前端环境为什么越来越复杂
查看>>
Node-RED中Button按钮组件和TextInput文字输入组件的使用
查看>>