js_Array类型_操作方法

2021-04-10 06:28

阅读:659

标签:gre   range   操作   log   返回   lis   list   参数   影响   

目录

  • concat
  • slice
  • splice
    • ex.删除第一项
    • ex.删除连续的两项
    • ex.删除一项,并在那个位置插入两项

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类型_操作方法

标签:gre   range   操作   log   返回   lis   list   参数   影响   

原文地址:https://www.cnblogs.com/Syinho/p/12397637.html

上一篇:Hadoop HDFS web

下一篇:xml与ajax1


评论


亲,登录后才可以留言!