Ich habe var ar = [1, 2, 3, 4, 5]und möchte eine Funktion getSubarray(array, fromIndex, toIndex), das Ergebnis des Aufrufs getSubarray(ar, 1, 3)ist ein neues Array [2, 3, 4].
Ich habe var ar = [1, 2, 3, 4, 5]und möchte eine Funktion getSubarray(array, fromIndex, toIndex), das Ergebnis des Aufrufs getSubarray(ar, 1, 3)ist ein neues Array [2, 3, 4].
Antworten:
Schau es dir an Array.slice(begin, end)
const ar = [1, 2, 3, 4, 5];
// slice from 1..3 - add 1 as the end index is not included
const ar2 = ar.slice(1, 3 + 1);
console.log(ar2);
arunverändert ist. console.log(ar); // -> [1, 2, 3, 4, 5]
Verwenden Sie für eine einfache Verwendung von slicemeine Erweiterung für Array-Klasse:
Array.prototype.subarray = function(start, end) {
if (!end) { end = -1; }
return this.slice(start, this.length + 1 - (end * -1));
};
Dann:
var bigArr = ["a", "b", "c", "fd", "ze"];
Test1 :
bigArr.subarray(1, -1);
<["b", "c", "fd", "ze"]
Test2:
bigArr.subarray(2, -2);
<["c", "fd"]
Test3:
bigArr.subarray(2);
<["c", "fd", "ze"]
Könnte für Entwickler aus einer anderen Sprache (z. B. Groovy) einfacher sein.
Array.prototype.containsin erzwingtArray.prototype.includes .
subarrayMethode unerwartete Ergebnisse liefert. bigArr.slice(1,-1)gibt zurück ['b','c','fd'], was Sie erwarten würden (-1 schlägt ein Element vom Ende des neuen Arrays ab). Aber bigArr.subarray(1,-1)kehrt das gleiche wie bigArr.subarray(1), was zu sagen ist alles von der Position 1 bis zum Ende bigArr. Sie zwingen Benutzer außerdem, immer negative Zahlen als endParameter anzugeben. Irgendeinend >= -1 ergibt das gleiche Ergebnis wie wann end === undefined. Auf der anderen Seite bigArr.slice(1,3)kehrt zurück['b','c'] , was wiederum erwartet wird.
const array_one = [11, 22, 33, 44, 55];
const start = 1;
const end = array_one.length - 1;
const array_2 = array_one.slice(start, end);
console.log(array_2);
Die Frage ist tatsächlich nach einem neuen Array gefragt , daher glaube ich, dass eine bessere Lösung darin besteht, die Antwort von Abdennour TOUMI mit einer Klonfunktion zu kombinieren :
function clone(obj) {
if (null == obj || "object" != typeof obj) return obj;
const copy = obj.constructor();
for (const attr in obj) {
if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr];
}
return copy;
}
// With the `clone()` function, you can now do the following:
Array.prototype.subarray = function(start, end) {
if (!end) {
end = this.length;
}
const newArray = clone(this);
return newArray.slice(start, end);
};
// Without a copy you will lose your original array.
// **Example:**
const array = [1, 2, 3, 4, 5];
console.log(array.subarray(2)); // print the subarray [3, 4, 5, subarray: function]
console.log(array); // print the original array [1, 2, 3, 4, 5, subarray: function]
[ http://stackoverflow.com/questions/728360/most-elegant-way-to-clone-a-javascript-object]
Array.prototype.slicegibt bereits eine Kopie zurück. Array.prototype.spliceÄndert das ursprüngliche Array.
slicebereits eine flache Kopie zurückgegeben, sodass diese subarrayImplementierung nicht erforderlich ist. Es ist aber auch erwähnenswert, dass Sie ein eingebautes Objekt mit Affen gepatcht haben, was ein großes Nein-Nein ist. Siehe die Kommentare zur Antwort von Abdennour TOUMI .