xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

使用一行 js 写出简洁、高效、优雅的代码技巧指南 All In One

使用一行 js 写出简洁、高效、优雅的代码技巧指南 All In One

🚀 优雅指数 ⭐️⭐️⭐️⭐️⭐️

⭐️⭐️⭐️⭐️⭐️
🌟🌟🌟🌟🌟
🎖🎖🎖🎖🎖

💩 shit 指数 💩💩💩💩💩
💩💩💩💩💩
⚠️⚠️⚠️⚠️⚠️
☢️☢️☢️☢️☢️
☣️☣️☣️☣️☣️

image

数组

  1. 自动生成指定长度和指定类型的测试数据

生成 1 ~ 100 数字(number)

const arr = [...``.padEnd(100, ` `)].map((_, i) => i + 1);
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]

// 🚀 优雅指数 ⭐️⭐️⭐️⭐️⭐️
const arr = [...new Uint8Array(100).map((_, i) => i + 1)];
// const arr = [...new Uint8Array(100)].map((_, i) => i + 1);

// 💩💩💩💩💩  手动写死
const arr =  [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100];

// 💩💩💩 for 循环
const arr = [];
for(let i = 0; i < 100; i++) {
  arr.push(i + 1);
}

// 💩💩💩 while 循环
const arr = [];
let i = 0;
while(i < 100) {
  arr.push(i + 1);
  i++;
}

生成 1 ~ 100 字符串(string)

const arr = [...``.padEnd(100, ` `)].map((_, i) => `${i + 1}`);
// ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '90', '91', '92', '93', '94', '95', '96', '97', '98', '99', '100']

// 🚀 优雅指数 ⭐️⭐️⭐️⭐️⭐️
const arr = [...new Uint8Array(100).map((_, i) => `${i + 1}`)];
// const arr = [...new Uint8Array(100)].map((_, i) => `${i + 1}`);

// 💩💩💩💩💩  手动写死
const arr = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '90', '91', '92', '93', '94', '95', '96', '97', '98', '99', '100']

// 💩💩💩 for 循环
const arr = [];
for(let i = 0; i < 100; i++) {
  arr.push(`${i + 1}`);
}

// 💩💩💩 while 循环
const arr = [];
let i = 0;
while(i < 100) {
  arr.push(`${i + 1}`);
  i++;
}

  1. 同一个数组中的两个项目交换数据 (swap)
const arr = [1,2,3,4,5];
// [1, 2, 3, 4, 5]
// 🚀 优雅指数 ⭐️⭐️⭐️⭐️⭐️
[arr[0], arr[4]] = [arr[4], arr[0]];
// [5, 1]
arr;
// [5, 2, 3, 4, 1]
const arr = [1,2,3,4,5];
// [1, 2, 3, 4, 5]
// 💩💩💩💩💩
const temp = arr[0];
arr[0] = arr[4]];
arr[4] = temp;

arr;
// [5, 2, 3, 4, 1]

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

refs

one-line-awesome-js-code

one line of efficient, concise, elegant and awesome javascript code collection

一行高效的、简洁的、优雅的、超棒的 javascript 代码集合

https://github.com/xgqfrms/one-line-awesome-js-code



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2023-02-13 15:59  xgqfrms  阅读(38)  评论(2编辑  收藏  举报