• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
LilyLiya
博客园    首页    新随笔    联系   管理    订阅  订阅
Array
mixed array; 数组基本操作: push, pop, shift, unshift, slice, splice
  • Arrays: ordered collections of values
    • List of comments on IG post
    • Collection of levels in a game
    • songs in a playlist
  • Creating Arrays
let students = [];  //empty array

let colors = ['red', 'orange', 'yellow']; // strings

let lottoNums = [19,22,34];  //numbers

let stuff = [true, 68, 'cat', null];   //mixed array
  • arrays can be modified
let colors = ['red', 'blue', 'green'];

color[0] = 'red';

color[3] = 'blue';
  • Array methods
    • push, add to end
    • pop, remove from end
    • shift, remove from start
    • unshift, add to start
    • concat, merge arrays
    • includes, look for a value
    • indexOf, just like string.indexOf
    • join, creats a string from an array
    • reverse, reverses an array
    • slice, copies a portion on an array
    • splice, removes/replaces elements
    • sort, sort an array
  • Use const to define an array, as long as the reference remains the same, we can change the values of an array
  • const Eggs = ['brown', 'brown'];
    Eggs.push('purple');
    Eggs[0] = 'green';
    
    
    
    Eggs = ['nlue', 'pink']; //ERROR ! the reference was changed

     

  • nested arrays

    const board = [
         ['O', null, 'X'],
         ['H', 'A', 'N'],    
    ]
    
    
    board[1][0] = 'H'

     

posted on 2021-01-08 07:14  LilyLiya  阅读(87)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3