就决定是你了 - 阵列系列III

来到阵列系列的最後一天,今天要一次认识会改变原本阵列的Array Method,再往下看之前,先来个想像力练习,首先,把阵列想成一个书柜,之後要放进去的元素则是一本本的书。好了,那我们开始吧~

使用目的 方法名称 回传值
增加阵列元素(至阵列末端) Array.prototype.push() 阵列的新长度
增加阵列元素(至阵列起始) Array.prototype.unshift() 阵列的新长度
移除阵列最後一个元素 Array.prototype.pop() 移除的元素,如果是空阵列,则回传 undefined
移除阵列第一个元素 Array.prototype.shift() 移除的元素,如果是空阵列,则回传 undefined
增加/移除阵列元素(可自行指定位置) Array.prototype.splice() 被删除的元素阵列,如果没有删除任何元素则回传空阵列
要将阵列有规律的排序 Array.prototype.sort() 排序後的阵列
将阵列的排序颠倒 Array.prototype.reverse() 反转後的阵列


图片来源:Unsplash

将书放到书柜

  • Array.prototype.push() **括号内放入要加入的元素
const classics = ['The Trial', '1984', 'The Great Gatsby', 'Les Misérables'];

// 在书柜空白区增加两本书
const classicsLength = classics.push('On the Road', 'Waiting for Godot');

console.log(classicsLength); // 6
console.log(classics);  // ['The Trial', '1984', 'The Great Gatsby', 'Les Misérables', 'On the Road', 'Waiting for Godot']

  • Array.prototype.unshift() **括号内放入要加入的元素
const classics = ['The Trial', '1984', 'The Great Gatsby', 'Les Misérables'];

// 把书往右侧推,然後再放入一本书
const classicsLength = classics.unshift('On the Road');

console.log(classicsLength); // 5
console.log(classics); // ['On the Road', 'The Trial', '1984', 'The Great Gatsby', 'Les Misérables']

从书柜取书

  • Array.prototype.pop()
const classics = ['The Trial', '1984', 'The Great Gatsby', 'Les Misérables']

// 拿掉最後一本书
const removed = classics.pop();

console.log(removed);  // Les Misérables
console.log(classics); // [ 'The Trial', '1984', 'The Great Gatsby' ]
  • Array.prototype.shift()
const classics = ['The Trial', '1984', 'The Great Gatsby', 'Les Misérables'];

// 拿掉第一本书
const removed = classics.shift();

console.log(removed);  // The Trial
console.log(classics); // [ '1984', 'The Great Gatsby', 'Les Misérables' ]

放书,取书,把书乱塞

  • Array.prototype.splice(改动的元素索引, 删除的元素个素, 要加入的元素)

const classics = ['The Trial', '1984', 'The Great Gatsby', 'Les Misérables'];

// 从1984那本书开始,拿走两本书
const removed = classics.splice(1, 2);

console.log(removed); // [ '1984', 'The Great Gatsby' ]
console.log(classics); // [ 'The Trial', 'Les Misérables' ]


const classics = ['The Trial', '1984', 'The Great Gatsby', 'Les Misérables'];

// 从1984那本书开始,拿走两本书,再塞入On the Road那本书
const removed = classics.splice(1, 2, 'On the Road');

console.log(removed); // [ '1984', 'The Great Gatsby' ]
console.log(classics); // [ 'The Trial', 'On the Road', 'Les Misérables' ]

整理书

  • Array.prototype.reverse()

const classics = ['The Trial', '1984', 'The Great Gatsby', 'Les Misérables'];


classics.reverse();
console.log(classics); // [ 'Les Misérables', 'The Great Gatsby', '1984', 'The Trial' ]

  • Array.prototype.sort() **括号内可选择性地放入定义排序的函式

注:假设要针对书名做排序,可是如果书名是以冠词(The/A/An)开头的话,则以书名的第二个单字做排序依据。


const bookTitles = ['The Trial', 'Normal People', 'The Great Gatsby', 'Give and Take', 'Think Twice', 'Animal Farm', 'An American Odyssey']

// 移除书名的冠词
function removeArticle(bookTitle) {
  return bookTitle.replace(/^(a |an |the )/i, '').trim();
}

// 由A到Z排序
bookTitles.sort((current, next) => {
  if(removeArticle(current) > removeArticle(next)) {
    return 1;
  } else {
    return -1;
  };
});

/images/emoticon/emoticon19.gif
不知道replace和trim的朋友,推荐你来个String Methods套餐
不知道/^(a |an |the )/i 是什麽魔法咒语的朋友,咱们明天中秋节一起来研究正规表达式吧~

参考资料:
Array - MDN
JavaScript — Array mutable methods


<<:  [区块链&DAPP介绍 Day12] Solidity 教学 - contracts-1

>>:  线性串列的循环/双向链式储存 - DAY 6

中华职棒全垒打王传奇 | 鹰侠 廖敏雄 林仲秋 吕明赐 坎沙诺... 你还记得谁? | 每年红不让最多的男人们

鹰侠、廖敏雄、林仲秋、吕明赐、坎沙诺...这些名字对你有什麽意义?他们可都是职棒元年後,每年的全垒...

【*】AI Go Senior 补充 (2021)

环境安装 在使用Python开发AI时,由於需时时查看处理中的训练资料,於是大多使用Jupyter ...

试试照本宣科地打造新创企业

在创业的过程中,往往会不知所措,不知道下一步该怎麽走 也不清楚这条路的最後会长成什麽样子 正如大家最...

[第八天]从0开始的UnityAR手机游戏开发-如何将模型设置在图卡上和脚本解说

将模型设置在图卡上 先将ImageTarget下的子物件删除 在Project找到从商店下载的模型...

[Day15] Tableau 轻松学 - 地图工作表

前言 我们已经学会使用长条图来做资料探索。然而,Tableau Desktop 除了长条图外,还有其...