aboutsummaryrefslogtreecommitdiff
path: root/files/ar/web/javascript/reference/global_objects/array/slice/index.html
blob: c0e4bde2c28782f4f3e36c91e72c67de38ab5b35 (plain)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
---
title: Array.prototype.slice()
slug: Web/JavaScript/Reference/Global_Objects/Array/slice
tags:
  - المصفوفات
  - جافا سكريبت
translation_of: Web/JavaScript/Reference/Global_Objects/Array/slice
---
<div>{{JSRef}}</div>

<p dir="rtl">ال <code><strong>slice()</strong></code> method إرجاع نسخة ضئيلة من جزء من مصفوفة إلى object مصفوفة جديد تم تحديده من <code>start</code> إلى <code>end</code> (<code>end</code> غير مضمنة) بينما <code>start</code> و <code>end</code> تمثلان مؤشر العناصر في هذه المصفوفة. لن يتم تعديل المصفوفة الأصلية.</p>

<div>{{EmbedInteractiveExample("pages/js/array-slice.html")}}</div>

<p class="hidden">The source for this interactive demo is stored in a GitHub repository. If you'd like to contribute to the interactive demo project, please clone <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> and send us a pull request.</p>

<h2 dir="rtl" id="تركيب_الجملة">تركيب الجملة</h2>

<pre class="syntaxbox notranslate"><var>arr</var>.slice([<var>start</var>[, <var>end</var>]])
</pre>

<h3 dir="rtl" id="المعاملات">المعاملات</h3>

<dl>
 <dt><code><var>start</var></code> {{optional_inline}}</dt>
 <dd dir="rtl">مؤشر ذو أساس صفري يبدأ فيه الاستخراج.</dd>
 <dd dir="rtl">يمكن استخدام مؤشر سلبي يشير إلى إزاحة من نهاية التسلسل. <code>slice(-2)</code> يستخرج آخر عنصرين في التسلسل.</dd>
 <dd dir="rtl">إذا كانت <code><var>start</var></code> غير محددة, تبدأ<code>slice</code> من المؤشر <code>0</code>.</dd>
 <dd dir="rtl">إذا كانت <code><var>start</var></code> is أكبر من نطاق فهرس التسلسل ، يتم إرجاع صفيف فارغ.</dd>
 <dt><code><var>end</var></code> {{optional_inline}}</dt>
 <dd dir="rtl">مؤشر ذو أساس صفري قبل أن ينتهي الاستخراج. <code>slice</code> مستخرجات إلى ولا تشمل <code><var>end</var></code>. على سبيل المثال, <code>slice(1,4)</code> يستخرج العنصر الثاني من خلال العنصر الرابع (العناصر المفهرسة 1 و 2 و 3).</dd>
 <dd dir="rtl">يمكن استخدام مؤشر سلبي يشير إلى إزاحة من نهاية التسلسل. <code>slice(2,-1)</code> يستخرج العنصر الثالث من خلال العنصر الثاني إلى الأخير في التسلسل.</dd>
 <dd dir="rtl">إذا تم حذف <code><var>end</var></code>, <code>slice</code> مستخرجات من خلال نهاية التسلسل(<code><var>arr</var>.length</code>).</dd>
 <dd dir="rtl">اذا كانت <code><var>end</var></code> أكبر من طول التسلسل,  فإن<code>slice</code> تستخرج حتى نهاية التسلسل(<code><var>arr</var>.length</code>).</dd>
</dl>

<h3 id="القيمة_العائدة">القيمة العائدة</h3>

<p dir="rtl">مصفوفة جديدة تحتوي على العناصر المستخرجة.</p>

<h2 id="الوصف">الوصف</h2>

<p dir="rtl"><code>slice</code> لا تغير المصفوفة الأصلية. تقوم بإرجاع نسخة ضئيلة من العناصر من المصفوفة الأصلية. يتم نسخ عناصر الصفيف الأصلي في الصفيف الذي تم إرجاعه كما يلي:</p>

<ul>
 <li dir="rtl">للreference Object(وليس الobject الفعلي) ،تقوم <code>slice</code> بنسخ reference object إلى المصفوفة الجديدة الجديد. يشير كل من المصفوفة الأصلية والجديدة إلى نفس ال object. إذا تغير reference Object ، تكون التغييرات مرئية لكل من المصفوفات الجديدة والأصلية.</li>
 <li dir="rtl">للأرقام و الحروف والقيم المنطقية strings, numbers and booleans (not {{jsxref("String")}}, {{jsxref("Number")}} and {{jsxref("Boolean")}} objects),تقوم <code>slice</code> بنسخ القيم إلى مصفوفة جديدة. لا تؤثر التغييرات على الحرف أو الرقم أو القيمة المنطقية في مصفوفة على المصفوفة الآخرى.</li>
</ul>

<p dir="rtl">إذا تمت إضافة عنصر جديد إلى أي مصفوفة ، فلن تتأثر المصفوفة الآخرى.</p>

<h2 dir="rtl" id="أمثلة">أمثلة</h2>

<h3 dir="rtl" id="إعادة_جزء_من_من_مصفوفة_موجودة">إعادة جزء من من مصفوفة موجودة</h3>

<pre class="brush: js notranslate">let fruits = ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
let citrus = fruits.slice(1, 3)

// fruits contains ['Banana', 'Orange', 'Lemon', 'Apple', 'Mango']
// citrus contains ['Orange','Lemon']
</pre>

<h3 dir="rtl" id="باستخدام_slice">باستخدام <code>slice</code></h3>

<p dir="rtl">في المثال التالي, تقوم<code>slice</code> بإنشاء مصفوفة جديدة <code>newCar</code>, من <code>myCar</code>. كلاهما يتضمن إشارة إلى الobject <code>myHonda</code>. عندما يتغير لون <code>myHonda</code> إلى الأرجواني, تعكس كلا المصفوفتان التغيير.</p>

<pre class="brush: js notranslate">// Using slice, create newCar from myCar.
let myHonda = { color: 'red', wheels: 4, engine: { cylinders: 4, size: 2.2 } }
let myCar = [myHonda, 2, 'cherry condition', 'purchased 1997']
let newCar = myCar.slice(0, 2)

// Display the values of myCar, newCar, and the color of myHonda
//  referenced from both arrays.
console.log('myCar = ' + JSON.stringify(myCar))
console.log('newCar = ' + JSON.stringify(newCar))
console.log('myCar[0].color = ' + myCar[0].color)
console.log('newCar[0].color = ' + newCar[0].color)

// Change the color of myHonda.
myHonda.color = 'purple'
console.log('The new color of my Honda is ' + myHonda.color)

// Display the color of myHonda referenced from both arrays.
console.log('myCar[0].color = ' + myCar[0].color)
console.log('newCar[0].color = ' + newCar[0].color)
</pre>

<p>This script writes:</p>

<pre class="notranslate">myCar = [{color: 'red', wheels: 4, engine: {cylinders: 4, size: 2.2}}, 2,
         'cherry condition', 'purchased 1997']
newCar = [{color: 'red', wheels: 4, engine: {cylinders: 4, size: 2.2}}, 2]
myCar[0].color = red
newCar[0].color = red
The new color of my Honda is purple
myCar[0].color = purple
newCar[0].color = purple
</pre>

<h3 id="Array-like_objects">Array-like objects</h3>

<p dir="rtl"><code>slice</code> method يمكن أيضًا استدعاؤها لتحويل  Array-like objects / مجموعات إلى مصفوفة جديدة. انت فقط {{jsxref("Function.prototype.bind", "bind")}} the method لل object. The {{jsxref("Functions/arguments", "arguments")}}داخل دالة هو مثال على 'array-like object'.</p>

<pre class="brush: js notranslate">function list() {
  return Array.prototype.slice.call(arguments)
}

let list1 = list(1, 2, 3) // [1, 2, 3]
</pre>

<p>البناء يمكن أن يتم ب {{jsxref("Function.prototype.call", "call()")}} method of {{jsxref("Function.prototype")}} ويمكن تقليلها باستخدام <code>[].slice.call(arguments)</code> بدلا من<code>Array.prototype.slice.call</code>.</p>

<p dir="rtl">على أي حال يمكن تبسيطها باستخدام {{jsxref("Function.prototype.bind", "bind")}}.</p>

<pre class="brush: js notranslate">let unboundSlice = Array.prototype.slice
let slice = Function.prototype.call.bind(unboundSlice)

function list() {
  return slice(arguments)
}

let list1 = list(1, 2, 3) // [1, 2, 3]</pre>

<h2 id="المواصفات">المواصفات</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">مواصفات</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-array.prototype.slice', 'Array.prototype.slice')}}</td>
  </tr>
 </tbody>
</table>

<h2 id="التوافق_مع_المتصفح">التوافق مع المتصفح</h2>

<div class="hidden">يتم إنشاء جدول التوافق في هذه الصفحة من البيانات المهيكلة. إذا كنت ترغب في المساهمة في البيانات ، يرجى مراجعة https://github.com/mdn/browser-compat-data وإرسال طلب سحب إلينا.</div>

<p>{{Compat("javascript.builtins.Array.slice")}}</p>

<h2 id="انظر_أيضا">انظر أيضا</h2>

<ul>
 <li>{{jsxref("Array.prototype.splice()")}}</li>
 <li>{{jsxref("Function.prototype.call()")}}</li>
 <li>{{jsxref("Function.prototype.bind()")}}</li>
</ul>