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
asked Feb 8, 2017 at 11:05
Gurmukh Singh
2,0474 gold badges32 silver badges74 bronze badges
-
2The latter should work with no problemhaim770– haim7702017年02月08日 11:08:26 +00:00Commented 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 everyoneGurmukh Singh– Gurmukh Singh2017年02月08日 11:42:32 +00:00Commented Feb 8, 2017 at 11:42
3 Answers 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.
answered Feb 8, 2017 at 11:14
jjj
1,1543 gold badges18 silver badges28 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
ibrahim mahrir
I don't think that's the problem!
Gurmukh Singh
my mistake,thats was a typo, the quotes are there
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
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;
}
answered Feb 8, 2017 at 11:14
ibrahim mahrir
31.8k5 gold badges50 silver badges78 bronze badges
Comments
lang-js