數(shù)組-不修改原數(shù)組

  • arr.slice( begin[, end] )
    var students = [
    {id: 1, score: 80},
    {id: 2, score: 50},
    {id: 3, score: 70}
    ];
    var newStudents = students.slice(0, 2);
    /*
    [
    {id: 1, score: 80},
    {id: 2, score: 50}
    ];
    */
  • arr.concat( value1, ..., valueN )
    var students1 = [
    {id: 1, score: 80},
    {id: 2, score: 50},
    {id: 3, score: 70}
    ];
    var students2 = [
    {id: 4, score: 90},
    {id: 5, score: 60}
    ];
    var students3 = [
    {id: 6, score: 40},
    {id: 7, score: 30}
    ];
    var newStudents = students1.concat(students2, students3);
    /*
    [
    {id: 1, score: 80},
    {id: 2, score: 50},
    {id: 3, score: 70},
    {id: 4, score: 90},
    {id: 5, score: 60},
    {id: 6, score: 40},
    {id: 7, score: 30}
    ];
    */
  • arr.join( [separator] )
    var emails = ["wq@163.com","gp@163.com","xl@163.com"];
    emails.join(";"); // => "wq@163.com;gp@163.com;xl@163.com"
  • arr.map( callback[, thisArg] )
    var scores = [60,70,80,90];
    var addScore = function (item, index, array) {
    return item + 5;
    }
    var newScores = scores.map(addScore); // => [65,75,85,95]
  • arr.reduce(callback[, initialValue])
    var students = [
    {id: 1, score: 80},
    {id: 2, score: 50},
    {id: 3, score: 70}
    ];
    var sum = function(previousResult, item, index, array) {
    return preiousResult + item.score;
    };
    students.reduce(sum, 0); // => 200
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容