Files
freeCodeCamp/curriculum/challenges/chinese-traditional/02-javascript-algorithms-and-data-structures/basic-javascript/access-array-data-with-indexes.md
2022-04-27 08:57:55 -07:00

1.7 KiB
Raw Blame History

id, title, challengeType, videoUrl, forumTopicId, dashedName
id title challengeType videoUrl forumTopicId dashedName
56bbb991ad1ed5201cd392ca 通過索引訪問數組中的數據 1 https://scrimba.com/c/cBZQbTz 16158 access-array-data-with-indexes

--description--

我們可以使用索引(indexes)來訪問數組中的數據。

數組索引與字符串一樣使用方括號來表示,不同的是,它們不是指定字符,而是指定數組中的一個條目。 數組索引與字符串索引一樣是從 0 開始(zero-based)的,所以數組中第一個元素的索引編號是 0


示例

const array = [50, 60, 70];
console.log(array[0]);
const data = array[1];

console.log(array[0]) 打印 50data 的值爲 60

--instructions--

創建一個名爲 myData 的變量,並使用括號表示法將其設置爲等於 myArray 的第一個值。

--hints--

變量 myData 應該等於 myArray 的第一個值。

assert(
  (function () {
    if (
      typeof myArray !== 'undefined' &&
      typeof myData !== 'undefined' &&
      myArray[0] === myData
    ) {
      return true;
    } else {
      return false;
    }
  })()
);

應使用括號表示法訪問變量 myArray 中的數據。

assert(
  (function () {
    if (code.match(/\s*=\s*myArray\[0\]/g)) {
      return true;
    } else {
      return false;
    }
  })()
);

--seed--

--after-user-code--

if(typeof myArray !== "undefined" && typeof myData !== "undefined"){(function(y,z){return 'myArray = ' + JSON.stringify(y) + ', myData = ' + JSON.stringify(z);})(myArray, myData);}

--seed-contents--

const myArray = [50, 60, 70];


--solutions--

const myArray = [50, 60, 70];
const myData = myArray[0];