我有一个JavaScript对象,如下所示:

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"}
]};

如果我想要向该列表添加/删除项目,我应该如何使用jQuery呢?客户端希望该列表是可动态修改的.

推荐答案

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.

您的代码定义了一个对象(data),其中包含一个对象数组(items)(每个对象都有idnametype).

You don't need or want jQuery for this, just JavaScript.

Adding an item:

data.items.push(
    {id: "7", name: "Douglas Adams", type: "comedy"}
);

这增加了结局.参见下面的中间添加.

Removing an item:

有几种方法.splice法是最通用的:

data.items.splice(1, 3); // Removes three items starting with the 2nd,
                         // ("Witches of Eastwick", "X-Men", "Ordinary People")

splice修改原始数组,并返回已删除项的array.

Adding in the middle:

splice actually does both adding and removing. The signature of the splice method is:

removed_items = arrayObject.splice(index, num_to_remove[, add1[, add2[, ...]]]);
  • index - the index at which to start making changes
  • num_to_remove-从该索引开始,删除这么多条目
  • addN - ...and then insert these elements

所以我可以在第三个位置添加一个项目,如下所示:

data.items.splice(2, 0,
    {id: "7", name: "Douglas Adams", type: "comedy"}
);

它的意思是:从索引2开始,删除零项,然后插入以下项.结果如下:

var data = {items: [
    {id: "1", name: "Snatch", type: "crime"},
    {id: "2", name: "Witches of Eastwick", type: "comedy"},
    {id: "7", name: "Douglas Adams", type: "comedy"},     // <== The new item
    {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"}
]};

You can remove some and add some at once:

data.items.splice(1, 3,
    {id: "7", name: "Douglas Adams", type: "comedy"},
    {id: "8", name: "Dick Francis", type: "mystery"}
);

...which means: Starting at index 1, remove three entries, then add these two entries. Which results in:

var data = {items: [
    {id: "1", name: "Snatch", type: "crime"},
    {id: "7", name: "Douglas Adams", type: "comedy"},
    {id: "8", name: "Dick Francis", type: "mystery"},
    {id: "4", name: "Ordinary People", type: "drama"},
    {id: "5", name: "Billy Elliot", type: "drama"},
    {id: "6", name: "Toy Story", type: "children"}
]};

Jquery相关问答推荐

使用 JQuery 在 span 标签中用逗号分隔页面上的文本

Jquery 表数据无法在 html 上正确显示

如何在外部JS文件中使用带有参数的laravel路由

如何使用 jQuery 的 drop 事件上传从桌面拖动的文件?

jQuery:获取数据属性

jQuery中的多个 Select 器链接?

Twitter Bootstrap 是否包含 jQuery?

JQuery通过类名获取所有元素

JQuery/Javascript:判断 var 是否存在

数据表警告(表 id = 'example'):无法重新初始化数据表

jQuery $.browser 是否已弃用?

jQuery 中 prop() 和 attr() 的区别以及何时使用 attr() 和 prop()

使用 JQuery 更改 :before css Select 器的宽度属性

根据弹出框相对于窗口边缘的 X 位置更改 Bootstrap 弹出框的位置?

字符串化(转换为 JSON)具有循环引用的 JavaScript 对象

无法更新数据属性值

检测 jQuery UI 对话框是否打开

如何在 JavaScript / jQuery 中获取对象的属性?

如何让 jQuery 等到效果完成?

AJAX 成功中的 $(this) 不起作用