slice()
بأن نكون انتقائيين حول عناصر المصفوفة المراد نسخها ، من بين العديد من المهام المفيدة الأخرى ، يتيح لنا مشغل التوزيع الجديد لـ ES6 نسخ جميع عناصر الصفيف بسهولة ، بالترتيب ، مع بناء بسيط وقابل للقراءة للغاية. تبدو صيغة الانتشار بهذا الشكل: ...
الناحية العملية ، يمكننا استخدام عامل الانتشار لنسخ مصفوفة مثل: السماح لهذاالصورة = [true، true، undefined، false، null]؛
السماح أن AArray = [... thisArray]؛
// thatArray يساوي [true ، true ، غير محدد ، false ، فارغ]
/ / هذا لا يزال يظل بدون تغيير ، وهو مطابق لذلك
copyMachine
والتي تأخذ arr
(صفيف) و num
(a number) كوسيطة. من المفترض أن تقوم الدالة بإرجاع صفيف جديد يتكون من نسخ num
من arr
. لقد قمنا بمعظم العمل لك ، لكنه لا يعمل بشكل صحيح بعد. قم بتعديل الوظيفة باستخدام صيغة الانتشار بحيث تعمل بشكل صحيح (تلميح: قد تكون طريقة أخرى قمنا بتغطيتها بالفعل مفيدة هنا!). copyMachine([true, false, true], 2)
[[true, false, true], [true, false, true]]
'
testString: 'assert.deepEqual(copyMachine([true, false, true], 2), [[true, false, true], [true, false, true]], "copyMachine([true, false, true], 2)
should return [[true, false, true], [true, false, true]]
");'
- text: 'copyMachine([1, 2, 3], 5)
[[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]
'
testString: 'assert.deepEqual(copyMachine([1, 2, 3], 5), [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]], "copyMachine([1, 2, 3], 5)
should return [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]
");'
- text: 'copyMachine([true, true, null], 1)
يجب أن يعيد [[true, true, null]]
'
testString: 'assert.deepEqual(copyMachine([true, true, null], 1), [[true, true, null]], "copyMachine([true, true, null], 1)
should return [[true, true, null]]
");'
- text: 'copyMachine(["it works"], 3)
[["it works"], ["it works"], ["it works"]]
'
testString: 'assert.deepEqual(copyMachine(["it works"], 3), [["it works"], ["it works"], ["it works"]], "copyMachine(["it works"], 3)
should return [["it works"], ["it works"], ["it works"]]
");'
- text: و copyMachine
وظيفة يجب الاستفادة من spread operator
مع مجموعة arr
testString: 'assert.notStrictEqual(copyMachine.toString().indexOf(".concat(_toConsumableArray(arr))"), -1, "The copyMachine
function should utilize the spread operator
with array arr
");'
```