1

I need to remove an object from an JSON tree. I know a reference to that object. Is there a nice way to do it via JavaScript or jQuery besides traversing the whole tree?

Example:

    party = {
    "uuid": "4D326531-3C67-4CD2-95F4-D1708CE6C7A8",
    "link": {
        "rel": "self",
        "href": "http://localhost:8080/cim/party/4D326531-3C67-4CD2-95F4-D1708CE6C7A8"
    },
    "type": "PERSON",
    "name": "John Doe",

    "properties": {
        "CONTACT": [            
            {
                "category": "CONTACT",
                "type": "EMAIL",
                "key": "email",
                "value": "[email protected]",
                "id": "27DDFF6E-5235-46BF-A349-67BEC92D6DAD"
            },
            {
                "category": "CONTACT",
                "type": "PHONE",
                "key": "mobile",
                "value": "+43 999 999990 3999",
                "id": "6FDAA4C6-9340-4F11-9118-F0BC514B0D77"
            }
        ],
        "CLIENT_DATA": [
            {
                "category": "CLIENT_DATA",
                "type": "TYPE",
                "key": "client_type",
                "value": "private",
                "id": "65697515-43A0-4D80-AE90-F13F347A6E68"
            }
        ]
    },
    "links": []
    }

And i have a reference: contact = party.properties.contact[1]. And I want to do something like delete contact.

0

2 Answers 2

1

You may delete it this way. I just tested it.

var party = {
    // ...
}

alert(party.properties.CONTACT[0]) // object Object
delete party.properties.CONTACT[0] // true
alert(party.properties.CONTACT[0]) // undefined

Fiddle

UPDATE

In the case above party is a direct property of window object

window.hasOwnProperty('party'); // true

and that's why you can't delete a property by reference. Anyhow, behavior of delete operator with host objects is unpredictable. Though, you may create a scope around the party object and then you'll be allowed to delete it.

var _scope = {};
var _scope.party = {
    // ...
};

var r = _scope.party.properties.CONTACT[0];
window.hasOwnProperty('party'); // false
alert(r) // object Object
delete r // true
alert(r) // undefined
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, this works, but I cannot use the reference. Anyhow I can do something like this: var properties = $scope.party.properties[category]; var propIdx = $.inArray(property, properties); properties.splice(propIdx, 1);
@Marius Check the updated answer. I believe things are clear now :)
1

It only works one way: a variable holds a reference, but there is no way given a particular reference to infer what variables hold it (without iterating over them and comparing).

2 Comments

Ok, is then any why to find an item index from and Array by reference. For exemple find(party.properties.contact, ctc) = 1 where ctc = party.properties.contact[1]
@Marius: yep, just iterate over array and compare.

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.