美文网首页
数组-修改原数组

数组-修改原数组

作者: zz张哲 | 来源:发表于2016-09-27 15:14 被阅读0次
  • arr.indexOf( searchElement[, formindex=0] )
    var telephones = [110,120,114];
    telephones.indexOf(120); // => 1
    telephones.indexOf(119); // => -1
  • arr.forEach( callback[, thisArg] )
    var students = [
    {id: 1, score: 80},
    {id: 2, score: 50},
    {id: 3, score: 70}
    ];
    var editScore = function (item, index, array) {
    item.score += 5;
    };
    students.forEach(editScore);
    students[0].score; // => 85
  • arr.reverse()
    var students = [
    {id: 1, score: 80},
    {id: 2, score: 50},
    {id: 3, score: 70}
    ];
    students.reverse();
    students[0].score; // => 70
  • arr.sort( [compareFunction] )
    var students = [
    {id: 1, score: 80},
    {id: 2, score: 50},
    {id: 3, score: 70}
    ];
    var byScore = function (a, b) {
    return b.score - a.score;
    }
    /*
    [
    {id: 1, score: 80},
    {id: 2, score: 50},
    {id: 3, score: 70}
    ];
    */
  • arr.push( element1, ..., elementN )
    var students = [
    {id: 1, score: 80},
    {id: 2, score: 50},
    {id: 3, score: 70}
    ];
    students.push({id: 4, score: 90});
    /*
    [
    {id: 1, score: 80},
    {id: 2, score: 50},
    {id: 3, score: 70},
    {id: 4, score: 90}
    ];
    */
  • arr.unshift( element1, ..., elementN )
    var students = [
    {id: 1, score: 80},
    {id: 2, score: 50},
    {id: 3, score: 70}
    ];
    students.unshift({id: 4, score: 90});
    /*
    [
    {id: 4, score: 90}
    {id: 1, score: 80},
    {id: 2, score: 50},
    {id: 3, score: 70}
    ];
    */
  • arr.shift()
    var students = [
    {id: 1, score: 80},
    {id: 2, score: 50},
    {id: 3, score: 70}
    ];
    students.shift();
    /*
    [
    {id: 2, score: 50},
    {id: 3, score: 70}
    ];
    */
  • arr.pop()
    var students = [
    {id: 1, score: 80},
    {id: 2, score: 50},
    {id: 3, score: 70}
    ];
    students.pop();
    /*
    [
    {id: 1, score: 80},
    {id: 2, score: 50}
    ];
    */
  • srr.splice( index, howMany[, ele1[, ...[, eleN]]] )
    var students = [
    {id: 1, score: 80},
    {id: 2, score: 50},
    {id: 3, score: 70}
    ];
    students.splice(1, 1, {id: 4, score: 90});
    /*
    [
    {id: 1, score: 80},
    {id: 4, score: 90},
    {id: 3, score: 70}
    ];
    /
    students.splice(1, 0, {id: 4, score: 90});
    /

    [
    {id: 1, score: 80},
    {id: 4, score: 90},
    {id: 2, score: 50},
    {id: 3, score: 70}
    ];
    /
    students.splice(1, 1);
    /

    [
    {id: 1, score: 80},
    {id: 3, score: 70}
    ];
    */

相关文章

  • 数组打乱的代码片段

    打乱给定数组,返回新数组,不修改原数组

  • 数组-修改原数组

    arr.indexOf( searchElement[, formindex=0] )var telephones...

  • js数组操作

    一、改变原数组的方法 a.reverse() 将数组逆序,会修改原数组 a.sort 对数组进行排序, 需要传入自...

  • 数组-不修改原数组

    arr.slice( begin[, end] )var students = [{id: 1, score: 8...

  • js操作数组,不修改原数组

    js数组是引用类型的值,我们直接将原数组赋值给新的变量,由于指针指向同一个内存地址,修改数组,原数组也会被修改。 ...

  • react map use

    map 类似foreach 返回一个新的数组,不会修改原数组

  • 关于vue中数组与对象更新检测的问题

    (1)数组更新检测 然而有些非变异数组方法不会修改原数组:filter、concat、slice这些操作并不会修改...

  • javascript数组方法超全总结

    concat():对数组进行拼接,具有打散插入的数组的功能,返回新数组,无权修改原数组。如果给这个concat传递...

  • js

    1.数组API 1)string() 把数组转为字符串 //不修改原数组 2.join('') 拼接,把数组中的元...

  • 3. js数组去重方法

    数组去重 删除数组中的重复项; 方法一:注意indexOf的兼容 方法二:修改原数组 方法二优化:

网友评论

      本文标题:数组-修改原数组

      本文链接:https://www.haomeiwen.com/subject/jtwcyttx.html