0

I am using titanium for developing Android application. I want to delete some data from json object. My json object given below:

{"feeds":
[
    {"username":"abc","user":"abc","feed":{"description":"dss","id":660,"user_id":1}},
    {"username":"bcd","user":"bcd","feed":{"description":"dddd","id":659,"user_id":1}}
]
}

for receiving json object I used following code

var json = this.responseText;
var json = JSON.parse(json);
json.feeds.splice(0,1);
alert(json.feeds[0]);

I want to delete particular data from json object like json.feeds[0] using JavaScript. I am able to access json.feeds[0] but not able to delete. Is there any way to delete that data from json object using JavaScript?

2 Answers 2

3

You are using splice to properly remove an element from a javascript array:

json.feeds.splice(0,1)

Using the code you've provided it will look like:

(function(){
    var json = {
        "feeds": [
            {"username":"abc","user":"abc","feed":{"description":"dss","id":660,"user_id":1}},
            {"username":"bcd","user":"bcd","feed":{"description":"dddd","id":659,"user_id":1}}
        ]
    };

    json.feeds.splice(0,1);
    console.log(json.feeds); // just to check that "feeds" contains only a single element
})();
Sign up to request clarification or add additional context in comments.

6 Comments

thank u for quick replay. I tried this but it not working.it not deleting data from json object. I also tried delete json.feeds[0] but that also not working
I've updated the answer with a working snippet based on your input. Is it what you are actually want to achieve?
thank you for reply.I have updated my code in question.plz check it's not working as required.
Actually, it is. As your array in question contains two elements, after deleting the first one, the second one is shifted and becomes the first. I.e. initially you have json.feeds[0] and json.feeds[1], after splice the first one is deleted and the second one is shifted. That still allows you to access json.feeds[0], however, that will be the element that was initially the second. The first one has gone. You'd better check json.feeds.length for array length before and after splice operation.
You are telling as I required.But after splice it showing me 0th position element not 1st position element and also not changing length.i.e. it remain 2. is there any problem in my json object? I can also modify the field in the data but only can't delete it.
|
3
  1. Parse the JSON into a JavaScript data structure
  2. Use splice to remove the elements you don't want from the array
  3. Serialise the JavaScript objects back into JSON

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.