数组-修改原数组

  • 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}
    ];
    */

你可能感兴趣的:(数组-修改原数组)