zoukankan      html  css  js  c++  java
  • xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

    js splice vs slice

    splice

    insert, replcae, add, delete

    
    // let arrDeletedItems = array.splice(start[, deleteCount[, item1[, item2[, ...]]]])
    const months = ['1', '2', '3'];
    
    // insert, delete 0
    months.splice(1, 0, '5');// ["1", "5", "2", "3"]
    console.log(months);
    
    // replace, delete 1
    months.splice(3, 1, '6');// ["1", "5", "2", "6"]
    console.log(months);
    
    // add, delete -1
    months.splice(4, 1, '7');// ["1", "5", "2", "6", "7"]
    console.log(months);
    
    // delete, delete 1
    months.splice(4, 1);// ["1", "5", "2", "6"]
    console.log(months);
    
    

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

    demo

    https://stackoverflow.com/questions/37601282/javascript-array-splice-vs-slice

    https://www.w3schools.com/jsref/jsref_slice_string.asp

    
    let dataGET = [
        {
            index: 0,
            keyword: "a",
            value: "1",
            description: "a=1",
            operate: ["edit", "save", "cancel"],
            isEdit: false,
        },
        {
            index: 1,
            keyword: "b",
            value: "2",
            description: "b=1",
            operate: ["edit", "save", "cancel"],
            isEdit: false,
        },
        {
            index: 2,
            keyword: "c",
            value: "3",
            description: "c=1",
            operate: ["edit", "save", "cancel"],
            isEdit: false,
        },
    ];
    
    // let arr = dataGET.slice(1, 1);
    
    // console.log(arr);
    
    //splice
    var array=[1,2,3,4,5];
    console.log(array.splice(2, 1));
    
    //slice
    var array2=[1,2,3,4,5]
    console.log(array2.slice(2, 1));
    
    
    console.log("----after-----");
    console.log(array);
    console.log(array2);
    
    
    
    

    splice & delete Array item by index

    index = 2

    
    //splice & will modify the origin array
    const arr1 = [1,2,3,4,5];
    //slice & won't modify the origin array
    const arr2 = [1,2,3,4,5]
    
    console.log("----before-----");
    console.log(arr1.splice(2, 1));
    console.log(arr2.slice(2, 1));
    
    console.log("----after-----");
    console.log(arr1);
    console.log(arr2);
    
    

    refs



    ©xgqfrms 2012-2020

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


  • 相关阅读:
    机械学习--5
    机械学习--4
    机械学习--3
    机械学习--2
    机器学习--1
    编译原理 作业十五
    编译原理 作业十四
    编译原理 作业十二
    编译原理 作业十一
    编译原理 作业十
  • 原文地址:https://www.cnblogs.com/xgqfrms/p/10711789.html
Copyright © 2011-2022 走看看