2.9 KiB
2.9 KiB
id, title, challengeType, forumTopicId, localeTitle
id | title | challengeType | forumTopicId | localeTitle |
---|---|---|---|---|
587d7b7e367417b2b2512b20 | Use an Array to Store a Collection of Data | 1 | 301167 | 使用数组存储数据集合 |
Description
let simpleArray = ['one', 2, 'three', true, false, undefined, null];
console.log(simpleArray.length);
// logs 7
可以在上述例子中看到,所有数组都有一个长度(length)属性。可以简单地使用Array.length
方法来访问它。
下面是一个关于数组的更复杂的例子。这是一个多维数组(multi-dimensional Array),或者说是一个包含了其他数组的数组。可以注意到,在它的内部还包含了 JavaScript 中的对象(objects)结构。我们会在后面的小节中讨论该数据结构,但现在你只需要知道数组能够存储复杂的对象类型数据。
let complexArray = [
[
{
one: 1,
two: 2
},
{
three: 3,
four: 4
}
],
[
{
a: "a",
b: "b"
},
{
c: "c",
d: "d"
}
]
];
Instructions
yourArray
的变量。请修改题目中的语句,将一个含有至少 5 个元素的数组赋值给yourArray
变量。你的数组应该包含至少一个 string 类型的数据、一个 number 类型的数据和一个 boolean 类型的数据。
Tests
tests:
- text: yourArray 应该是一个数组。
testString: assert.strictEqual(Array.isArray(yourArray), true);
- text: <code>yourArray</code>至少要包含 5 个元素。
testString: assert.isAtLeast(yourArray.length, 5);
- text: <code>yourArray</code>应该包含至少一个<code>boolean</code>。
testString: assert(yourArray.filter( el => typeof el === 'boolean').length >= 1);
- text: <code>yourArray</code>应该包含至少一个<code>number</code>。
testString: assert(yourArray.filter( el => typeof el === 'number').length >= 1);
- text: <code>yourArray</code>应该包含至少一个<code>string</code>。
testString: assert(yourArray.filter( el => typeof el === 'string').length >= 1);
Challenge Seed
let yourArray; // change this line
Solution
let yourArray = ['a string', 100, true, ['one', 2], 'another string'];