本文的重点是强调我选择在 JavaScript 中进行评估的主要原因之一:原生数组方法。在本文中,我将创建一个快速备忘录,以帮助您和我在未来的测试中取得好成绩。
提示:如果您正在准备编码面试,我真的鼓励您在 CodeWars 练习您的开发技能。那里的提示很容易成为我评估的提示之一。这非常相似。
说了这么多,让我们开始吧。
let array1 = [1,2];let array2 = [3,4];let array3 = [5,6,7];let array4 = array1.concat(array2, array3)console.log(array4)// [1,2,3,4,5,6,7]
将两个或多个数组连接成一个数组。
您可以在 concat 方法中拥有无限数量的参数。
还可以这样做:
let array1 = [1,2];let array2 = [3,4];let array3 = [5,6,7];let array4 = [...array1, ...array2]console.log(array4);
var name = "Kyle";console.log(name.length) // 4var array = [1,2,3,4,5];console.log(array.length); //5
用于获取数组或字符串的长度
let students = ["Anthony", "Beth", "Cersi" , "Dario", "Elizabeth", "Farrah"];let welcomeMessage = "Hello " + students.join(" and ")console.log(welcomeMessage);/* PRINTSHello Anthony and Beth and Cersi and Dario and Elizabeth and Farrah*/
将数组的元素连接在一起并将其转换为字符串
join方法的一个参数(分隔符)
例如,我们使用“和”来连接数组中的所有元素。你可以使用空格“”或逗号“,”或任何你想要的。
let students = ["Anthony", "Beth", "Cersi" , "Dario", "Elizabeth", "Farrah"];let removed = students.pop();console.log(removed); //Farrahconsole.log(students); //["Anthony", "Beth", "Cersi" , "Dario", "Elizabeth"]students.pop();console.log(students); //["Anthony", "Beth", "Cersi" , "Dario"]
移除数组最后的元素
该方法返回被移除的元素,这是可选的,您也可以单独使用 students.pop() 。
let students = ["Anthony", "Beth", "Cersi" , "Dario", "Elizabeth", "Farrah"];students.push("George");console.log(students);/* Prints: ["Anthony", "Beth", "Cersi" , "Dario", "Elizabeth", "Farrah", "George]*/
在数组末尾添加元素
let students = ["Anthony", "Beth", "Cersi" , "Dario", "Elizabeth", "Farrah"];students.shift();console.log(students);/*PRINTS:["Beth", "Cersi" , "Dario", "Elizabeth", "Farrah"]*/
删除数组最开始的元素。
let students = ["Anthony", "Beth", "Cersi" , "Dario", "Elizabeth", "Farrah"];students.unshift("Zander");console.log(students);/*PRINTS:["Zander", "Anthony", "Beth", "Cersi" , "Dario", "Elizabeth", "Farrah"]*/
var name = "Jennifer Aniston"var firstName = name.slice(0,8);console.log(firstName); //Jennifervar array = [1,2,3,4,5,6,7];console.log(array.slice(0,3)); //[1,2,3]var array = ["Index 1", "Index 2", "Index 3", "Index 4"];console.log(array.slice(1)); //[ 'Index 2', 'Index 3', 'Index 4' ]
切片方法的一个参数
切片方法的两个参数:(开始索引,结束索引+1)
let array1 = [1,2,3,6,7,6,7,8,9];array1.splice(3, 6)console.log(array1); //[ 1, 2, 3 ]
用于添加、删除或替换数组中的元素
拼接方法的两个参数
let array1 = [1,2,3,6,7,6,7,8,9];array1.splice(3, 2, 4, 5 )console.log(array1); // [1,2,3,4,5,6,7,8,9]
拼接方法的三个或更多参数
let array1 = [1,2,3,6,7,6,7,8,9];console.log(array1.reverse()); // [ 9, 8, 7, 6, 7, 6, 3, 2, 1 ]
反转数组的顺序
//升序排序let array1 = [5, 1, 8, 3];array1.sort((a, b) => a-b);console.log(array1) //[ 1, 3, 5, 8 ]//递减数字排序let array1 = [5, 1, 8, 3];array1.sort((a, b) => b-a);console.log(array1) //[ 8, 5, 3, 1 ]//递减数字排序let array1 = [5, 1, 8, 3];array1.sort((a, b) => a-b).reverse();console.log(array1) //[ 8, 5, 3, 1 ]//升序字母排序let array1 = ["apples", "carrots", "zendaya", "tapioca"];array1.sort();console.log(array1)//降序字母排序let array1 = ["apples", "carrots", "zendaya", "tapioca"];array1.sort().reverse();console.log(array1)
let name = "Kyle";let age = 21;let city = "Los Angeles"let sentence = name.concat(" is " , age , " years old and lives in ", city);console.log(sentence);
将两个或多个字符串连接成一个字符串。
您可以在 concat 方法中拥有无限数量的参数。您也可以用 + 代替 ,它分隔每个字符串/变量
let sentence = "Publish today on Medium";console.log(sentence.indexOf("today")) //8
查找字符串第一次出现的索引
如果未找到字符串,则返回 -1
let sentence = "a dog went to a dog park on a tuesday night. ";console.log(sentence.lastIndexOf("a ")); //28
查找字符串最后一次出现的索引
如果未找到字符串,则返回 -1
let name = "Kyle DeGuzman";let nameArray = name.split(" ");console.log(nameArray) //['Kyle', 'DeGuzman']let alphabet = "abcdefghijklmnopqrstuv";let alphabetArray = alphabet.split("");console.log(alphabetArray) //['a','b','c','d','e','f','g','h'....]let favoriteThings = "Raindrops on Roses, Whiskers on Kittens, Bright Copper Kettles, Warm Woolen Mittens, Brown Paper Packages Tied Up With String";let favoriteThingsArray = favoriteThings.split(",");console.log(favoriteThingsArray)
将字符串转换为数组
let name = "kYle";let name = name.toLowerCase(); console.log(name); //kylelet string = ("triumph").toLowerCase(); console.log(string); //triumph
将字符串中的所有字母转换为小写
如果您正在测试用户输入的字符串,这非常棒。
例如:
if (input == "apple"){...}
所以如果用户输入Apple、aApple、appLE等,它们将是无效的。
if (input.toLowerCase() == "apple"){}
这是一个更好的方法
let name = "kYle";let name = name.toUpperCase(); console.log(name); //KYLElet string = ("triumph").toUpperCase(); console.log(string); //TRIUMPH
将字符串中的所有字母转换为大写
let firstName = "Kyle
";let lastName = "Deguzman ";console.log(firstName + lastName);console.log(firstName.trim() + lastName).
从字符串的左侧和右侧删除空格。
以上就是我今天整理的18个关于JavaScript数组的方法,希望你能从这个清单列表中学习到新知识,如果你觉得我今天的内容对你有帮助的话,请记得点赞我,关注我,并将它分享给你身边的朋友,也许能够帮助到他。
当然,这个清单列表也只是其中的一部分而已,如果你还有要补充的知识内容,欢迎你在留言区给我留言。
最后,感谢你的阅读,祝编程愉快!
来源: web前端开发
留言与评论(共有 0 条评论) “” |