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.
This question is related to
javascript
jquery
object-literal
Keep things simple :)
var my_form_obj = {};
my_form_obj.name = "Captain America";
my_form_obj.weapon = "Shield";
Hope this helps!
Adding an object in a json array
var arrList = [];
var arr = {};
arr['worker_id'] = worker_id;
arr['worker_nm'] = worker_nm;
arrList.push(arr);
Removing an object from a json
It worker for me.
arrList = $.grep(arrList, function (e) {
if(e.worker_id == worker_id) {
return false;
} else {
return true;
}
});
It returns an array without that object.
Hope it helps.
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:
// The values to put in the item
var id = 7;
var name = "The usual suspects";
var type = "crime";
// Create the item using the values
var item = { id: id, name: name, type: type };
// Add the item to the array
data.items.push(item);
If you are using jQuery you can use the extend function to add new items.
var olddata = {"fruit":{"apples":10,"pears":21}};
var newdata = {};
newdata['vegetables'] = {"carrots": 2, "potatoes" : 5};
$.extend(true, olddata, newdata);
This will generate:
{"fruit":{"apples":10,"pears":21}, "vegetables":{"carrots":2,"potatoes":5}};
Well, it's just a javascript object, so you can manipulate data.items
just like you would an ordinary array. If you do:
data.items.pop();
your items
array will be 1 item shorter.
Try
data.items.pop();
data.items.push({id: "7", name: "Matrix", type: "adult"});
var data = {items: [_x000D_
{id: "1", name: "Snatch", type: "crime"},_x000D_
{id: "2", name: "Witches of Eastwick", type: "comedy"},_x000D_
{id: "3", name: "X-Men", type: "action"},_x000D_
{id: "4", name: "Ordinary People", type: "drama"},_x000D_
{id: "5", name: "Billy Elliot", type: "drama"},_x000D_
{id: "6", name: "Toy Story", type: "children"}_x000D_
]};_x000D_
_x000D_
data.items.pop();_x000D_
data.items.push({id: "7", name: "Matrix", type: "adult"});_x000D_
_x000D_
console.log(data);
_x000D_
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 :
data.items = jQuery.grep(
data.items,
function (item,index) {
return item.id != "1";
});
DELETE Way:
delete data.items[0]
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.
Source: Stackoverflow.com