删除JSON的第一个元素 [英] Remove First element of JSON
问题描述
在我的JSON下面,我想删除其项目的第一行,例如:
Her s the below my JSON is nd I want to remove the first line of its items, like coming as:
data.items = {"username":"usr1","profile": "usr2", "items":[{"s": "1","f": "usr2","m": "hey ebteween how u doing","fr":"usr1"}]}
更新#1
$.each(data.items, function(i,item){
data.items.item.shift();
}
想要这样
{"username":"usr1","profile": "usr2", "items":[]}
我正在尝试使用data.items.slice(1)
方法,但是由于某种原因它不起作用:
I am trying using the data.items.slice(1)
method but somehow it is not working:
更新#2:
我通过添加shift()
尝试了代码,但是在控制台中我收到了:
i tried the code by adding the shift()
but in console i received:
console.log(data.items); data.items.shift();
console.log(data.items);data.items.shift();
[Object { s="1", f="usr1", m="m", more...}]
但是实际上它并没有从json对象中删除该元素
but actually it did not removed the element from the json object
推荐答案
要删除元素,可以使用删除:
to delete an element, you could use delete:
// it'll delete the first element of the json
delete data.items[0];
如果要删除指定密钥,则必须使用密钥名称:
if you want to delete a specify key, you have to use key name:
// delete the username key:
delete data.items["username"];
在您的情况下,您可以通过这种方式再次设置密钥:
in your case you could just set again your key in this way:
data.items["items"] = [];
您已将密钥设置为空数组
you have set your key with an empty array
这篇关于删除JSON的第一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!