1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
---
title: Array.prototype.shift()
slug: Web/JavaScript/Reference/Global_Objects/Array/shift
tags:
- Array
- JavaScript
- Method
- Prototype
translation_of: Web/JavaScript/Reference/Global_Objects/Array/shift
---
<div>{{JSRef}}</div>
<p>Die Methode <code><strong>shift()</strong></code> entfernt das <strong>erste</strong> Element eines Arrays und gibt dieses Element zurück. Diese Methode verändert die Länge des Arrays.</p>
<div>{{EmbedInteractiveExample("pages/js/array-shift.html")}}</div>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox"><code><var>arr</var>.shift()</code></pre>
<h3 id="Rückgabewert">Rückgabewert</h3>
<p>Das vom Array entfernte Element; {{jsxref("undefined")}} wenn das Array leer ist.</p>
<h2 id="Description" name="Description">Beschreibung</h2>
<p>Die Methode <code>shift</code> entfernt das Element an der nullten Stelle, verschiebt die übrigen Elemente um einen Index nach unten und gibt den Wert des entfernten Elementes zurück. Ist die {{jsxref("Array.length", "length")}} Eigenschaft gleich 0, wird {{jsxref("undefined")}} zurückgegeben.</p>
<p><code>shift</code> ist absichtlich generisch; diese Methode kann auf Array ähnlichen Objekten mit {{jsxref("Function.call", "call", "", 1)}} oder {{jsxref("Function.apply", "applied", "", 1)}} genutzt werden. <span id="result_box" lang="de"><span>Objekte, die keine </span></span><code>length</code> E<span lang="de"><span>igenschaft enthalten, die die letzte in einer Reihe von aufeinanderfolgenden, nullbasierten numerischen Eigenschaften widerspiegelt, verhalten sich möglicherweise nicht in einer sinnvollen Weise</span></span>.</p>
<h2 id="Examples" name="Examples">Beispiele</h2>
<h3 id="Example:_Removing_an_element_from_an_array" name="Example:_Removing_an_element_from_an_array">Ein Element eines Arrays entfernen</h3>
<p>Das folgende Skript zeigt das Array <code>myFish</code> bevor und nachdem das erste Element dieses Arrays entfernt wurde. Anschließend wird das entfernte Element angezeigt:</p>
<pre class="brush: js">var myFish = ['angel', 'clown', 'mandarin', 'surgeon'];
console.log('myFish before: ' + myFish);
// myFish before: ['angel', 'clown', 'mandarin', 'surgeon']
var shifted = myFish.shift();
console.log('myFish after: ' + myFish);
// myFish after: ['clown', 'mandarin', 'surgeon']
console.log('Removed this element: ' + shifted);
// Removed this element: angel</pre>
<h3 id="Einsatz_von_shift()_in_einer_while-Schleife">Einsatz von <code>shift()</code> in einer <code>while</code>-Schleife</h3>
<p>Die shift() Methode wird oft in der Bedingung in einer <code>while</code> Schleife verwendet. Im folgenden Beispiel wird in jeder Iteration das nächste Element vom Array entfernt, bis das Array leer ist.</p>
<pre class="brush: js">var names = ["Andrew", "Edward", "Paul", "Chris" ,"John"];
while( (i = names.shift()) !== undefined ) {
console.log(i);
}
// Andrew, Edward, Paul, Chris, John
</pre>
<h2 id="Specifications" name="Specifications">Spezifikationen</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Spezifikation</th>
<th scope="col">Status</th>
<th scope="col">Kommentar</th>
</tr>
<tr>
<td>{{SpecName('ES3')}}</td>
<td>{{Spec2('ES3')}}</td>
<td>Initiale Definition. Implementiert in JavaScript 1.2.</td>
</tr>
<tr>
<td>{{SpecName('ES5.1', '#sec-15.4.4.9', 'Array.prototype.shift')}}</td>
<td>{{Spec2('ES5.1')}}</td>
<td> </td>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-array.prototype.shift', 'Array.prototype.shift')}}</td>
<td>{{Spec2('ES6')}}</td>
<td> </td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-array.prototype.shift', 'Array.prototype.shift')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility" name="Browser_compatibility">Browserkompatibilität</h2>
<div>
<p>{{Compat("javascript.builtins.Array.shift")}}</p>
</div>
<h2 id="See_also" name="See_also">Siehe auch</h2>
<ul>
<li>{{jsxref("Array.prototype.push()")}}</li>
<li>{{jsxref("Array.prototype.pop()")}}</li>
<li>{{jsxref("Array.prototype.unshift()")}}</li>
<li>{{jsxref("Array.prototype.concat()")}}</li>
</ul>
|