freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-an.../basic-data-structures/create-complex-multi-dimens...

6.9 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
587d7b7b367417b2b2512b16 Create complex multi-dimensional arrays 1 创建复杂的多维数组

Description

真棒!你刚学了很多关于阵列的知识!这是一个相当高级别的概述,有很多关于使用数组的知识,其中大部分将在后面的章节中看到。但在继续观察对象之前 ,让我们再看一下,看看阵列如何变得比我们在之前的挑战中看到的更复杂。将数组视为数据结构时,最强大的功能之一是数组可以包含甚至完全由其他数组组成。我们已经看到在之前的挑战中包含数组的数组,但相当简单。但是,数组可以包含无限深度的数组,这些数组可以包含其他数组,每个数组都有自己的任意深度级别,依此类推。通过这种方式,数组可以非常快速地变成非常复杂的数据结构,称为多维或嵌套数组。请考虑以下示例:
让nestedArray = [// top或first level - 最外面的数组
['deep']//数组中的数组2个深度级别
[
['更深']['更深'] // 2个数组嵌套3层深
]
[
[
['deepest']['最深'] // 2个数组嵌套了4个级别
]
[
[
['deepest-est'] //一个数组嵌套5层深
]
]
]
]。
虽然这个例子看似令人费解,但在处理大量数据时,这种复杂程度并非闻所未闻,甚至不同寻常。但是,我们仍然可以使用括号表示法轻松访问此复杂数组的最深层次:
的console.lognestedArray [2] [1] [0] [0] [0];
//日志:最深?
现在我们知道那条数据在哪里,如果需要,我们可以重置它:
nestedArray [2] [1] [0] [0] [0] =“更深”;

的console.lognestedArray [2] [1] [0] [0] [0];
//现在记录:更深入

Instructions

我们已经定义了一个变量myNestedArray ,它等于一个数组。修改myNestedArray ,使用数据元素的字符串 数字布尔值的任意组合以便它具有五个深度级别请记住最外层的数组是级别1。某处在第三级上包括字符串'deep' ,在第四级,包括字符串'deeper' ,并且在第五层上,包括字符串'deepest'

Tests

tests:
  - text: <code>myNestedArray</code>应仅包含数字,布尔值和字符串作为数据元素
    testString: 'assert.strictEqual((function(arr) { let flattened = (function flatten(arr) { const flat = [].concat(...arr); return flat.some (Array.isArray) ? flatten(flat) : flat; })(arr); for (let i = 0; i < flattened.length; i++) { if ( typeof flattened[i] !== "number" && typeof flattened[i] !== "string" && typeof flattened[i] !== "boolean") { return false } } return true })(myNestedArray), true, "<code>myNestedArray</code> should contain only numbers, booleans, and strings as data elements");'
  - text: <code>myNestedArray</code>应该有5个级别的深度
    testString: 'assert.strictEqual((function(arr) {let depth = 0;function arrayDepth(array, i, d) { if (Array.isArray(array[i])) {  arrayDepth(array[i], 0, d + 1);} else {  depth = (d > depth) ? d : depth;}if (i < array.length) {  arrayDepth(array, i + 1, d);}  }arrayDepth(arr, 0, 0);return depth;})(myNestedArray), 4, "<code>myNestedArray</code> should have exactly 5 levels of depth");'
  - text: <code>myNestedArray</code>应该在嵌套3级深度的数组中恰好包含一个字符串<code>&quot;deep&quot;</code>
    testString: 'assert((function howDeep(array, target, depth = 0) {return array.reduce((combined, current) => {if (Array.isArray(current)) {  return combined.concat(howDeep(current, target, depth + 1));} else if (current === target) {  return combined.concat(depth);} else {  return combined;}}, []);})(myNestedArray, "deep").length === 1 && (function howDeep(array, target, depth = 0) {return array.reduce((combined, current) => {if (Array.isArray(current)) {  return combined.concat(howDeep(current, target, depth + 1));} else if (current === target) {  return combined.concat(depth);} else {  return combined;}}, []);})(myNestedArray, "deep")[0] === 2, "<code>myNestedArray</code> should contain exactly one occurrence of the string <code>"deep"</code> on an array nested 3 levels deep");'
  - text: <code>myNestedArray</code>应该在嵌套4级深度的数组中恰好包含一个字符串<code>&quot;deeper&quot;</code> deep”
    testString: 'assert((function howDeep(array, target, depth = 0) {return array.reduce((combined, current) => {if (Array.isArray(current)) {  return combined.concat(howDeep(current, target, depth + 1));} else if (current === target) {  return combined.concat(depth);} else {  return combined;}}, []);})(myNestedArray, "deeper").length === 1 && (function howDeep(array, target, depth = 0) {return array.reduce((combined, current) => {if (Array.isArray(current)) {  return combined.concat(howDeep(current, target, depth + 1));} else if (current === target) {  return combined.concat(depth);} else {  return combined;}}, []);})(myNestedArray, "deeper")[0] === 3, "<code>myNestedArray</code> should contain exactly one occurrence of the string <code>"deeper"</code> on an array nested 4 levels deep");'
  - text: <code>myNestedArray</code>应该在嵌套5级深度的数组中恰好包含一个字符串<code>&quot;deepest&quot;</code> <code>myNestedArray</code> <code>&quot;deepest&quot;</code>
    testString: 'assert((function howDeep(array, target, depth = 0) {return array.reduce((combined, current) => {if (Array.isArray(current)) {  return combined.concat(howDeep(current, target, depth + 1));} else if (current === target) {  return combined.concat(depth);} else {  return combined;}}, []);})(myNestedArray, "deepest").length === 1 && (function howDeep(array, target, depth = 0) {return array.reduce((combined, current) => {if (Array.isArray(current)) {  return combined.concat(howDeep(current, target, depth + 1));} else if (current === target) {  return combined.concat(depth);} else {  return combined;}}, []);})(myNestedArray, "deepest")[0] === 4, "<code>myNestedArray</code> should contain exactly one occurrence of the string <code>"deepest"</code> on an array nested 5 levels deep");'

Challenge Seed

let myNestedArray = [
  // change code below this line
  ['unshift', false, 1, 2, 3, 'complex', 'nested'],
  ['loop', 'shift', 6, 7, 1000, 'method'],
  ['concat', false, true, 'spread', 'array'],
  ['mutate', 1327.98, 'splice', 'slice', 'push'],
  ['iterate', 1.3849, 7, '8.4876', 'arbitrary', 'depth']
  // change code above this line
];

Solution

// solution required