From what I understood from your answers what you need is to filter the array:

Copyconst filteredArray = array.filter(element => {
    if (yourConditionIsTrue) { // if this element should be in the filteredArray
        return true;
    } else {
        return false
    }
});

Which can be done in one line:

Copyconst filteredArray = array.filter(element => conditionIsTrue);

This way your array remains untouched and you get a new array (filteredArray) only with the elements you need, but you don't mess up with the array you are iterating.

Answer from A. Llorente on Stack Overflow
🌐
TutorialsPoint
tutorialspoint.com › home › typescript › typescript array slice
TypeScript Array Slice
December 18, 2016 - TypeScript - null vs. undefined ... begin − Zero-based index at which to begin extraction. As a negative index, start indicates an offset from the end of the sequence. end − Zero-based index at which to end extraction. Returns the extracted array based on the passed parameters. var arr = ["orange", "mango", "banana", "sugar", "tea"]; console.log("arr.slice( 1, 2) : " + arr.slice( 1, 2) ); console.log("arr.slice( 1, 3) : " + arr.slice( 1, 3) );
🌐
Educative
educative.io › answers › what-is-arrayslice-in-typescript
What is array.slice() in TypeScript?
TypeScript is a superset of JavaScript. Wherever JavaScript runs, TypeScript runs, too. We can use the slice() method in TypeScript. This method is used to extract a certain section or parts of the elements of an array.
🌐
W3Schools
w3schools.com › jsref › jsref_slice_array.asp
JavaScript Array slice() Method
The slice() method returns selected elements in a new array.
🌐
GeeksforGeeks
geeksforgeeks.org › typescript › typescript-array-slice-method
TypeScript Array slice() Method - GeeksforGeeks
August 8, 2024 - Example 2: In this example we defines a string array arr, then uses the slice() method to extract portions of the array into val. ... // Driver code let arr: string[] = ["G", "e", "e", "k", "s", "f", "o", "r", "g", "e", "e", "k", "s"]; let val: string[]; // Use of slice() method val = arr.slice(2, 6); console.log(val); val = arr.slice(5, 8); console.log(val);
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › slice
Array.prototype.slice() - JavaScript | MDN
The slice() method of Array instances returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.
🌐
TutorialsPoint
tutorialspoint.com › home › typescript › typescript array splice method
TypeScript Array Splice Method
December 18, 2016 - Learn about the TypeScript array splice method, its syntax, and practical examples to manipulate arrays effectively.
Find elsewhere
🌐
W3Schools
w3schools.com › jsref › jsref_typed_slice.asp
JavaScript Typed Array slice() Method
The slice() method returns selected elements in an array, as a new array.
🌐
W3Schools
w3schools.com › js › tryit.asp
W3Schools online HTML editor
The W3Schools online code editor allows you to edit code and view the result in your browser
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › TypedArray › slice
TypedArray.prototype.slice() - JavaScript | MDN
This method is not generic and can only be called on typed array instances. ... const bytes = new Uint8Array([1, 2, 3]); bytes.slice(1); // Uint8Array [ 2, 3 ] bytes.slice(2); // Uint8Array [ 3 ] bytes.slice(-2); // Uint8Array [ 2, 3 ] bytes.slice(0, 1); // Uint8Array [ 1 ]
🌐
HowToDoInJava
howtodoinjava.com › home › typescript › typescript array slice()
TypeScript Array slice() with Examples
August 29, 2023 - In TypeScript, the array.slice() method extracts a section of an array (slice of an array) and returns it as a new array.
🌐
Built In
builtin.com › software-engineering-perspectives › array-slice-javascript
3 Ways to Use Array Slice in JavaScript | Built In
The splice() method can also be used remove elements at a specific index, but it modifies the original array. Using slice() allows you to achieve the same result without changing the source array.
🌐
TypeScript
typescriptlang.org › docs › handbook › 2 › everyday-types.html
TypeScript: Documentation - Everyday Types
For example, TypeScript knows that only a string value will have a typeof value "string": ... Notice that in the else branch, we don’t need to do anything special - if x wasn’t a string[], then it must have been a string. Sometimes you’ll have a union where all the members have something in common. For example, both arrays and strings have a slice ...
🌐
W3Schools Blog
w3schools.blog › home › javascript array slice()
JavaScript Array slice() - W3schools
April 20, 2019 - Returns: A new array with specified elements. ... <!DOCTYPE html> <html> <body> <script> var a = ["GOLD","SILVER","DIAMOND","RUBY","PLATINUM"] var result=a.slice(2,4); document.writeln(result); </script> </body> </html>
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript array methods › 3 pragmatic uses of javascript array slice() method
3 Pragmatic Uses of JavaScript Array slice() Method
May 8, 2024 - The Array.prototype object provides the slice() method that allows you to extract subset elements of an array and add them to the new array.
🌐
Codecademy
codecademy.com › docs › javascript › arrays › .slice()
JavaScript | Arrays | .slice() | Codecademy
May 22, 2025 - This means primitive values (like ... in the copied array will reflect in the original array. Yes, .slice() can be used to copy all or part of an array....
🌐
Fjolt
fjolt.com › article › javascript-slice
Javascript Array Slice Method
This is the index of the first item to include in your new array. For example, if set to 2, the slice will start your new array copy at index 2. If a negative is used, it indicates offset from the end of a sequence.