I have a Javascript object as follows:
var data = {items: [
{id: "1", name: "Snatch", type: "crime"},
{id: "2", name: "Witches of Eastwick", type: "comedy"},
{id: "3", name: "X-Men", type: "action"},
{id: "4", name: "Ordinary People", type: "drama"},
{id: "5", name: "Billy Elliot", type: "drama"},
{id: "6", name: "Toy Story", type: "children"}
]};
If I wanted to add/remove items to this list, how would I go about it using jQuery? The client wants this list to be dynamically modifiable.
First off, your quoted code is not JSON. Your code is JavaScript object literal notation. JSON is a subset of that designed for easier parsing.
Your code defines an object (
data
) containing an array (items
) of objects (each with anid
,name
, andtype
).You don't need or want jQuery for this, just JavaScript.
Adding an item:
That adds to the end. See below for adding in the middle.
Removing an item:
There are several ways. The
splice
method is the most versatile:splice
modifies the original array, and returns an array of the items you removed.Adding in the middle:
splice
actually does both adding and removing. The signature of thesplice
method is:index
- the index at which to start making changesnum_to_remove
- starting with that index, remove this many entriesaddN
- ...and then insert these elementsSo I can add an item in the 3rd position like this:
What that says is: Starting at index 2, remove zero items, and then insert this following item. The result looks like this:
You can remove some and add some at once:
...which means: Starting at index 1, remove three entries, then add these two entries. Which results in:
Adding an object in a json array
Removing an object from a json
It worker for me.
It returns an array without that object.
Hope it helps.
Splice is good, everyone explain splice so I didn't explain it. You can also use delete keyword in JavaScript, it's good. You can use $.grep also to manipulate this using jQuery.
The jQuery Way :
DELETE Way:
For Adding PUSH is better the splice, because splice is heavy weighted function. Splice create a new array , if you have a huge size of array then it may be troublesome. delete is sometime useful, after delete if you look for the length of the array then there is no change in length there. So use it wisely.
If you are using jQuery you can use the extend function to add new items.
This will generate:
Well, it's just a javascript object, so you can manipulate
data.items
just like you would an ordinary array. If you do:your
items
array will be 1 item shorter.That's not JSON at all, it's just Javascript objects. JSON is a text representation of data, that uses a subset of the Javascript syntax.
The reason that you can't find any information about manipulating JSON using jQuery is because jQuery has nothing that can do that, and it's generally not done at all. You manipulate the data in the form of Javascript objects, and then turn it into a JSON string if that is what you need. (jQuery does have methods for the conversion, though.)
What you have is simply an object that contains an array, so you can use all the knowledge that you already have. Just use
data.items
to access the array.For example, to add another item to the array using dynamic values: