0

Hi I have object like this

var obj= {
     _id: string;
    name: string;
    loc: [{
         locname: string;
         locId: string;
         locadd: [{
             st: string;
             zip: string;
         }]
     }]
}

Using typescript in angular 2 I want to delete a particular row

deleterow(i, j) {
  // i is index of loc 
  // j is index of locadd
  this.obj.loc.splice(i, 1) //works fine 
  this.obj.loc[i].locadd.splice(j, 1) //doesn't work. I don't get any error just 
  // row is not removed.
}

I am trying solution similar to given in this answer but doesn't work

JavaScript remove item from nested Array

Please let me know how I can remove the an item from locadd Thanks

6
  • What is j? "locadd" property has a single value .splice(0, 1). Given the value of "locadd" at the object at the question any value greater than 0 passed to first parameter of .slice() will not match any index within the array. Commented Feb 24, 2019 at 3:25
  • locadd is an array. j is index for locadd that I want to be removed. Commented Feb 24, 2019 at 3:26
  • Does "locadd" contain more than one element? Commented Feb 24, 2019 at 3:27
  • yes it is more than one element Commented Feb 24, 2019 at 3:28
  • Can you include the complete object and the expected result at the question and create a stacksnippets to demonstrate the issue? See stackoverflow.com/help/mcve Commented Feb 24, 2019 at 3:30

1 Answer 1

2

You deleted ith loc item. And then, You've referenced ith loc which is next of deleted loc item.

I think locadd should be deleted before delete loc.

this.obj.loc[i].locadd.splice(j,1)
this.obj.loc.splice(i, 1)

Update

I've made a snippet. It seems working.

var obj = {
  loc: [{
    locadd: [{
      st: '1',
      zip: '1',
    },{
      st: '2',
      zip: '2',
    },{
      st: '3',
      zip: '3',
    }]
  }]
};

function deleterow(i, j) {
  // i is index of loc 
  // j is index of locadd
  // this.obj.loc.splice(i, 1) //works fine 
  this.obj.loc[i].locadd.splice(j, 1) //doesn't work. I don't get any error just 
  // row is not removed.
}

console.log(this.obj.loc)
deleterow(0, 1)
console.log(this.obj.loc)

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

4 Comments

it doesn't work cause I am just trying this this.obj.loc[i].locadd.splice(j,1) The other this.obj.loc.splice(i,1) is just to show on first aray it works fine but nested it is not working
I was trying to edit your code snipped didn't know how to. Can you add a second element on locadd[0] than remove the one. Cause I believe that is where it doesn't work. Thanks
@J.Davidson Can you update your post what your intended by code?
Thank you for the snippet. Using it I was able to find out issue with j value, once corrected it worked fine. Thanks

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.