1

I have an array of items nested within another array of items. Im trying to remove a specific item within the nested array. I have the code 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[z].splice(z, 1);
        return;
    }
}
}

When i get to splice, an error is thrown saying splice is not a function. How can this be fixed? Also, this.transferedFeature is the correct one i need to remove.

3 Answers 3

1

It should be:

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

As this.featureGroups[i].features is the array within the array (based on your description).

Sign up to request clarification or add additional context in comments.

Comments

0

I think you are mistaking element at z to be an array, this is the correct code

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;
        }
    }
    }

Comments

0

It's all about use of indexing.

outerArr = [];
inner= [1,121,13];
outerArr.push(inner);
console.log(outerArr);
outerArr[0].splice(0,1)
console.log(outerArr)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.