js_Array类型_操作方法

Posted Syinho

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js_Array类型_操作方法相关的知识,希望对你有一定的参考价值。

concat

  • 1.创建当前数组的副本。2.将接收到的参数添加到这个副本的末尾。3.返回构建的新数组。
  • concat不影响原数组。
var colors=["red","green","blue"];
var colors2=colors.concat("yellow",["black","brown"]);
console.log(`colors:${colors}`);//["red","green","blue"]
console.log(`colors2:${colors2}`);//["red","green","blue",yellow","black","brown"]

slice

  • 基于当前数组创建新数组,但并不影响原数组。
  • 接收的参数:1.当前返回项的起始位置。2.返回项的结束位置。
var colors=["red","green","blue","black","brown"];
var colors2=colors.slice(1);
console.log(colors2);//["green","blue","black","brown"]
var colors3=colors.slice(1,4);
console.log(colors3);//["green","blue","black"]

splice

  • splice(选中的项的索引,以选中的项为首删除的项数,在选中的位置要插入的项)

ex.删除第一项

var colors=["red","green","blue"];
var removed=colors.splice(0,1);//选中索引为1的项,以它为首删除1个项
console.log(removed);//["red"]
console.log(colors);//["green","blue"]

ex.删除连续的两项

var colors=["red","green","blue"];
var removed=colors.splice(1,2);//选中索引为1的项,以它为首删除2个项
console.log(removed);//["green","blue"]
console.log(colors);//["red"]

ex.删除一项,并在那个位置插入两项

var colors=["red","green","blue"];
var removed=colors.splice(1,1,"yellow","orange");
console.log(removed);//["green"]
console.log(colors);//["red","yellow","orange","blue"]

以上是关于js_Array类型_操作方法的主要内容,如果未能解决你的问题,请参考以下文章

js_Array类型_迭代方法

js_Array类型_find和findIndex

片段“F1”不能在这里传播,因为“节点”类型的对象永远不能是“商店”类型

javaSE_07_方法

CodeIgniter - 加载视图问题

VSCode 配置 用户自定义代码片段 自定义自动代码补充