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 - Learn how to use the Array slice method in TypeScript to extract a portion of an array. Understand its parameters and practical examples.
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › typescript › typescript-array-slice-method
TypeScript Array slice() Method - GeeksforGeeks
August 8, 2024 - The Array.slice() method in TypeScript is a built-in function used to extract a section of an array and return it as a new array.
🌐
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.
🌐
Reddit
reddit.com › r/reactjs › how to slice an array to display the last 5 items?
How to slice an array to display the last 5 items? : r/reactjs
October 6, 2022 - Join the Reactiflux Discord (reactiflux.com) for additional React discussion and help. ... Sorry, this post was deleted by the person who originally posted it. Share ... Your .slice method call is set to return the first five users by returning users from index 0 to index 5. What you need to do is change the arguments you provide to .slice so it returns from the last index to the -5th position. You can Google how to return the last five elements in an array and any Javascript-related answer will work as well since you're using .slice().
🌐
Mimo
mimo.org › glossary › javascript › array-slice
JavaScript Array slice() Method: Syntax, Usage, and Examples
Calling slice() with no arguments returns a shallow copy of the entire array. ... This is especially useful when working with state in React or other frameworks that rely on immutability.
Find elsewhere
🌐
W3Schools
w3schools.com › jsref › jsref_slice_array.asp
JavaScript Array slice() Method
The slice() method returns selected elements in a new array.
🌐
Webdevtutor
webdevtutor.net › blog › typescript-array-slice-example
A Comprehensive Guide to TypeScript Array Slice with Examples
Learn how to effectively use additional props in TypeScript for React development to enhance code readability and maintainability.
🌐
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.
🌐
HackerNoon
hackernoon.com › how-to-use-the-javascript-slice-method-in-react
How to Use the JavaScript Slice() Method in React | HackerNoon
February 7, 2023 - The slice() method in JavaScript can be useful for extracting a portion of an array and returning a new one, which can be useful in various situations.
🌐
Ramesh Fadatare
rameshfadatare.com › home › typescript array slice()
TypeScript Array slice()
July 9, 2024 - In this chapter, we explored the slice() method for arrays in TypeScript, which is used to return a shallow copy of a portion of an array into a new array object selected from start to end. We covered its definition, syntax, parameters, return value, and provided several examples to demonstrate ...
🌐
Medium
windmaomao.medium.com › array-slice-in-javascript-is-super-handy-855237e9538f
Array slice in Javascript is super handy | by Fang Jin | Medium
December 19, 2023 - Array slice is really handy, especially for the parsing purpose. Moreover, it won’t touch your original array, so rest assured you won’t end up with too many bugs using it. ... Front-end Engineer, book author of “Designing React Hooks the Right Way” and "Think in Recursion"
🌐
React
react.dev › learn › updating-arrays-in-state
Updating Arrays in State – React
Sometimes, you may want to insert ... the ... array spread syntax together with the slice() method. The slice() method lets you cut a “slice” of the array....