Using Typescript to remove an element from an array inside another array

I've encountered an issue while trying to remove a specific item from a nested array of items within another array. Below is the code snippet:

removeFromOldFeatureGroup() {
for( let i= this.featureGroups.length-1; i>=0; i--) {
  if( this.featureGroups[i].featureGroupId == this.oldFeatureGroupId)
    for( let z= this.featureGroups[i].features.length-1; z>=0; z--) {
      if( this.featureGroups[i].features[z].featureId == this.featureId)
        this.transferedFeature = this.featureGroups[i].features[z];
        this.featureGroups[i].features.splice(z, 1);
        return;
    }
}
}

But when the code reaches the splice method, it throws an error saying that "splice is not a function". How can I correct this error? Additionally, the variable 'this.transferedFeature' contains the item I want to remove.

Answer №1

It needs to be:

this.featureGroups[i].features.splice(z, 1);

Since this.featureGroups[i].features represents the nested array according to your explanation.

Answer №2

It appears that there was a misunderstanding regarding the variable at z being treated as an array. The corrected code is shown below:

removeFromOldFeatureGroup() {
    for( let i = this.featureGroups.length - 1; i >= 0; i--) {
        if( this.featureGroups[i].featureGroupId === this.oldFeatureGroupId) {
            for( let z = this.featureGroups[i].features.length - 1; z >= 0; z--) {
                if( this.featureGroups[i].features[z].featureId === this.featureId) {
                    this.transferedFeature = this.featureGroups[i].features[z];
                    this.featureGroups[i].features.splice(z, 1);
                    return;
                }
            }
        }
    }
}

Answer №3

Understanding the power of indexing is key.

mainArray = [];
subArray= [5,55,15];
mainArray.push(subArray);
console.log(mainArray);
mainArray[0].splice(0,1)
console.log(mainArray)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When receiving JSON and attempting to store the data in a variable, I encounter an issue where it returns the error message "undefined is not iterable (cannot read property Symbol

I'm currently integrating the Advice Slip API into my project. I am experiencing an issue when trying to store the JSON data in a variable like so: let advice; fetch("https://api.adviceslip.com/advice").then(response => response.json()). ...

Ways to adjust your selection to the space or new line before or after

$('button').on('click', function(){ const selection = window.getSelection(); selection?.modify('move', 'backward', 'word'); selection?.modify('extend', 'forward', 'to the next space ...

Capturing attention within react-bootstrap drop-downs and pop-ups

Currently, I'm focused on enhancing accessibility features and I have a specific goal in mind: to confine the focus within a popover/dropdown whenever it is opened. Working with react-bootstrap, my inquiry revolves around finding out if there's ...

Is it possible to remove the browsing history of user inputs in JavaScript?

I'm currently working on a Simon Says game where the level of difficulty increases as players correctly repeat the pattern. However, I encountered an issue with clearing the input history that shows previous patterns entered by the user. How can I res ...

Displaying an alert on a webpage that shows a label input using JavaScript and

I'm currently working with HTML5 and JavaScript and I'm facing a challenge. I want to create a feature where users can input any word into a label, and when they click on a button, an alert is triggered with the given text. However, despite my ...

Utilize the onClick event to access a method from a parent component in React

Looking for guidance on accessing a parent component's method in React using a child component? While props can achieve this, I'm exploring the option of triggering it with an onClick event, which seems to be causing issues. Here's a simple ...

Create a polling feature using a Grease Monkey script

I am looking for a way to run a Tamper Monkey script on a Facebook page that regularly checks a database for new data and performs certain actions. I have attempted to implement polling using AJAX, and below is the code I used: (function poll() { setT ...

Dealing with Memory Leakage in C Programming

Currently working with GTK and struggling with the malloc() function. Valgrind is showing a memory leak, so I'm wondering what I'm doing wrong. Initially, I created a three-dimensional array by defining a pointer to a pointer to a pointer to a GT ...

Creating a PHP script that retrieves data from JavaScript and stores it in MySQL can be accomplished by using AJAX to send the

Hello, I am attempting to create a PHP script that can extract coordinates from this JavaScript code (or from this link ) and store them in a MySQL database. Can someone please provide me with a tutorial on how to accomplish this? <script> var ...

Creating an axios URL using Vue data results in receiving the value of undefined

I'm currently experimenting with axios to retrieve data from openweathermap. I've been working on constructing the URL by utilizing various methods to extract latitude and longitude from the user's browser, followed by a function call to pie ...

Issue with second button in Angular Onclick() not been resolved

I'm experiencing an issue with the onClick() methods in my code. There are two onClick() methods present on the UI, but when I click on the second one, it does not call the method while the first one does. I am struggling to figure out why this is hap ...

Create a simple carousel using only vanilla JavaScript, without relying on any external plugins

Currently, I am working on implementing a carousel using plain JavaScript without the use of any plugins. My goal is to have previous and next buttons that will control the sliding images. var firstval = 0; function ...

Is there a way for me to retrieve the bodyHeight attribute of ag-grid using public variables or data?

Working on a project using ag-grid community react. The main feature is a scrollable section filled with data, which can range from one piece to millions of pieces. I'm also incorporating a footer component for the grid that needs to adjust its height ...

not capable of outputting findings in a sequential manner

I am encountering an issue where my result is not printing line by line, instead everything shows up on a single line. How can I resolve this problem? Here is the code snippet I have tried: <script> function know(){ var num = Number(doc ...

When making a POST request using formData through AJAX, the $_POST variable remains empty

Below is the form: <form id="complaintForm" enctype="multipart/form-data" method="POST"> <input type="number" id="isComplaintForm" value="1" hidden> <label for="fname&q ...

Creating a File and Resource Manager for Your Express Application: Step-by-Step Guide

I'm interested in creating a website section where users can upload files, such as game mods, for others to download. I envision this section being able to handle a large volume of files and users. How can I achieve this scalability and what architect ...

Unlock the key to connecting the output of one observable to another in rxjs

I have a procedure for saving users in the database. These are the steps I take: 1) First, I validate the request. 2) Next, I hash the password. 3) Finally, I store the user details in the users collection along with the hashed password. Below is the ...

The fetch function in my Node.js code is failing to properly display JSON data from a file on the console

I am currently exploring node.js and working on building a web server to fetch a list of team members from a JSON file in the 'json' folder. However, I am encountering an issue with my fetch function as it is not displaying the data on the consol ...

Introduce new material at the start of each line, akin to what ::before accomplishes for the initial line

I am currently working on customizing the appearance of my <code>/<pre> tags while ensuring that users can still easily highlight and copy the code. The method I had in mind (shown below) only applies to the first line and doesn't repeat ...

What is the best way to access the original observed node using MutationObserver when the subtree option is set to

Is there a way to access the original target node when using MutationObserver with options set to childList: true and subtree: true? According to the documentation on MDN, the target node changes to the mutated node during callbacks, but I want to always ...