| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | --- | 
					
						
							|  |  |  | id: 56bbb991ad1ed5201cd392ce | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  | title: 使用 unshift() 操作数组 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | challengeType: 1 | 
					
						
							| 
									
										
										
										
											2020-04-29 18:29:13 +08:00
										 |  |  | videoUrl: 'https://scrimba.com/c/ckNDESv' | 
					
						
							|  |  |  | forumTopicId: 18239 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  | dashedName: manipulate-arrays-with-unshift | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | --- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | # --description--
 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  | 不仅可以 `shift`(移出)数组中的第一个元素,也可以 `unshift`(移入)一个元素到数组的头部。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  | `.unshift()` 函数用起来就像 `.push()` 函数一样,但不是在数组的末尾添加元素,`unshift()` 在数组的头部添加元素。 | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  | 示例: | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							| 
									
										
										
										
											2021-11-06 08:56:52 -07:00
										 |  |  | const ourArray = ["Stimpson", "J", "cat"]; | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  | ourArray.shift(); | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  | ourArray.unshift("Happy"); | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  | 在 `shift`、`ourArray` 后值为 `["J", "cat"]`。 在 `unshift`、`ourArray` 后值为 `["Happy", "J", "cat"]`。 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | # --instructions--
 | 
					
						
							| 
									
										
										
										
											2020-04-29 18:29:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-06 08:56:52 -07:00
										 |  |  | 使用 `unshift()` 将 `["Paul", 35]` 添加到 `myArray` 变量的开头。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | # --hints--
 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-14 21:20:39 -06:00
										 |  |  | `myArray` 现在应该等于 `[["Paul", 35], ["dog", 3]]`。 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | assert( | 
					
						
							|  |  |  |   (function (d) { | 
					
						
							|  |  |  |     if ( | 
					
						
							|  |  |  |       typeof d[0] === 'object' && | 
					
						
							|  |  |  |       d[0][0] == 'Paul' && | 
					
						
							|  |  |  |       d[0][1] === 35 && | 
					
						
							|  |  |  |       d[1][0] != undefined && | 
					
						
							|  |  |  |       d[1][0] == 'dog' && | 
					
						
							|  |  |  |       d[1][1] != undefined && | 
					
						
							|  |  |  |       d[1][1] == 3 | 
					
						
							|  |  |  |     ) { | 
					
						
							|  |  |  |       return true; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   })(myArray) | 
					
						
							|  |  |  | ); | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  | # --seed--
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## --after-user-code--
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | (function(y, z){return 'myArray = ' + JSON.stringify(y);})(myArray); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ## --seed-contents--
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							|  |  |  | // Setup | 
					
						
							| 
									
										
										
										
											2021-11-06 08:56:52 -07:00
										 |  |  | const myArray = [["John", 23], ["dog", 3]]; | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  | myArray.shift(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Only change code below this line | 
					
						
							| 
									
										
										
										
											2021-11-06 08:56:52 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | # --solutions--
 | 
					
						
							| 
									
										
										
										
											2020-04-29 18:29:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  | ```js | 
					
						
							| 
									
										
										
										
											2021-11-06 08:56:52 -07:00
										 |  |  | const myArray = [["John", 23], ["dog", 3]]; | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  | myArray.shift(); | 
					
						
							|  |  |  | myArray.unshift(["Paul", 35]); | 
					
						
							|  |  |  | ``` |