首页 > 编程语言 >[Javascript] avoid mutation: Array.prototype.toSpliced() vs splice()

[Javascript] avoid mutation: Array.prototype.toSpliced() vs splice()

时间:2023-05-02 14:13:08浏览次数:64  
标签:Feb May avoid Javascript Jan vs toSpliced Array prototype

Array.prototype.splice()mutates the original array. To avoid mutation, we use Array.prototype.slice().

new method Array.prototype.toSpliced() return a new array to avoid the mutation.

const months = ["Jan", "Mar", "Apr", "May"];

// Inserting an element at index 1
const months2 = months.toSpliced(1, 0, "Feb");
console.log(months2); // ["Jan", "Feb", "Mar", "Apr", "May"]

// Deleting two elements starting from index 2
const months3 = months2.toSpliced(2, 2);
console.log(months3); // ["Jan", "Feb", "May"]

// Replacing one element at index 1 with two new elements
const months4 = months3.toSpliced(1, 1, "Feb", "Mar");
console.log(months4); // ["Jan", "Feb", "Mar", "May"]

// Original array is not modified
console.log(months); // ["Jan", "Mar", "Apr", "May"]

 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toSpliced

标签:Feb,May,avoid,Javascript,Jan,vs,toSpliced,Array,prototype
From: https://www.cnblogs.com/Answer1215/p/17367620.html

相关文章

  • [Javascript] Avoid mutation, Array.prototype.toSorted() vs sort()
    sort(),mutatestheoriginalarray,andreturnthereferencetooriginalarrayandsorted.The toSorted() methodof Array instancesisthe copying versionofthe sort() method.Itreturnsanewarraywiththeelementssortedinascendingorder.const......
  • [Javascript] Avoid mutation, Array.prototype.toReversed() vs reverse()
    reverse()mutatestheoriginalarray,returnthereferencepointtotheoriginalarray.The toReversed() methodof Array instancesisthe copying counterpartofthe reverse() method.Itreturnsanewarraywiththeelementsinreversedorder.constite......
  • [Javascript] Array.prototype.with
    Prevously,whenwewanttoupateaniteminsideaarray:constitems=[{id:1,name:'a'},{id:2,name:'b'},{id:3,name:'c'},{id:4,name:'d'},{id:5,name:'e'}]constnewIt......
  • 前端进化笔记-JavaScript(二)
    因为作者学过其他类c语言,就不对大家都熟悉的内容进行赘述了。语法JavaScript区分大小写标识符:变量,函数,属性,函数参数的名称第一个字符必须是字母,下划线(_),美元符号($);关键字、保留字、true、false和null不能用作标识符作者在后续阅读的过程中,发现对各种名称不熟悉导致阅读不......
  • 一键部署VSCode_c环境脚本使用教程
       ......
  • 06 虚拟化Open vSwitch环境部署
    06虚拟化OpenvSwitch环境部署本节给出部署OpenvSwitch网桥环境的部署方法,使虚拟机连接到网络。6.1安装OpenvSwitch网桥OpenvSwitch网桥,具有更便捷的自动化编排能力。部署OpenvSwitch网桥需要安装网络虚拟化组件,这里介绍总体操作。6.1.1安装OpenvSwitch组件使用Openv......
  • vscode配置文件
    vscode用户配置文件{/*editor*/"editor.cursorBlinking":"smooth",//使编辑器光标的闪烁平滑,有呼吸感"editor.formatOnPaste":true,//在粘贴时格式化代码"editor.formatOnType":true,//敲完一行代码自动格式化"editor.smoothScrolling"......
  • JavaScript相关
    Javascript基础​ JavaScript,是一门能够运行在浏览器上的脚本语言.简称JS.首先,Javascript这个名字的由来就很有意思,不少人认为Javascript和Java貌似很像.容易想象成Java的脚本.但其实不然,两者之间没有任何关系.纯粹是商业碰瓷.​ 那么既然JS是可以运行在浏览器上......
  • SQLite vs Pandas
    AnalysisdetailsFortheanalysis,weranthesixtasks10timeseach,for5differentsamplesizes,foreachof3programs:pandas,sqlite,andmemory-sqlite(wheredatabaseisinmemoryinsteadofondisk).See below forthedefinitionsofeachtask.Ou......
  • 文心一言 VS chatgpt (13)-- 算法导论3.1 8题 3.2 1题
    八、可以扩展我们的记号到有两个参数n和m的情形,其中的n和m可以按不同速率独立地趋于无穷。对于给定的函数g(n,m),用O(g(n,m))来表示以下函数集:O(g(n,m))={f(n,m):存在正常量c、和,使得对所有n>=n0或m>=m0,有0<=f(n,m)<=cg(n,m)}对Ω(g(n,m))和θ(g(n,m))给出相应的定义。文......