0

I have an object, and within the object, i need to delete the address from the array of objects using javascript.

 obj = {
        "name":1,
        "Details":[
            {
              "mname":"text here",
              "sname":"text here",
              "address":"text",
              "saddress":"text"
            }
        ]
      }

I have tried the following, but no luck:

delete obj.Details.address

and

delete obj.Details[0].address
2
  • 2
    The latter should work with no problem Commented Feb 8, 2017 at 11:08
  • There must have been something wrong with the way i might have structured the project, but the latter is now working, thanks for your help everyone Commented Feb 8, 2017 at 11:42

3 Answers 3

3

your object structure is wrong

  obj = {
    "name":1,
    "Details":[
        {
          "mname":"text here",
          "sname":"text here",
          "address":"text",
          "saddress":"text"
        }
    ]
  }

it should be "address":"text", in string format then

delete obj.Details[0].address

will work.

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

2 Comments

I don't think that's the problem!
my mistake,thats was a typo, the quotes are there
1

Are you sure this don't work?

delete obj.Details[0].address

I've just tried in the chrome console and this works. Maybe you're not debugging correctly

Comments

1

If you want to delete the adress property of all the objects inside the Details array, then do it using forEach like this:

obj.Details.forEach(function(detail) {
    delete detail.address;
});

Or using an old for loop like this:

for(var i = 0; i < obj.Details.length; i++) {
    delete obj.Details[i].adress;
}

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.