admin管理员组文章数量:1327524
Her s the below my JSON is nd I want to remove the first line of its items, like ing as:
data.items = {"username":"usr1","profile": "usr2", "items":[{"s": "1","f": "usr2","m": "hey ebteween how u doing","fr":"usr1"}]}
Update #1
$.each(data.items, function(i,item){
data.items.item.shift();
}
want ito be like this
{"username":"usr1","profile": "usr2", "items":[]}
I am trying using the data.items.slice(1)
method but somehow it is not working:
Update #2:
i tried the code by adding the shift()
but in console i received:
console.log(data.items);data.items.shift();
[Object { s="1", f="usr1", m="m", more...}]
but actually it did not removed the element from the json object
Her s the below my JSON is nd I want to remove the first line of its items, like ing as:
data.items = {"username":"usr1","profile": "usr2", "items":[{"s": "1","f": "usr2","m": "hey ebteween how u doing","fr":"usr1"}]}
Update #1
$.each(data.items, function(i,item){
data.items.item.shift();
}
want ito be like this
{"username":"usr1","profile": "usr2", "items":[]}
I am trying using the data.items.slice(1)
method but somehow it is not working:
Update #2:
i tried the code by adding the shift()
but in console i received:
console.log(data.items);data.items.shift();
[Object { s="1", f="usr1", m="m", more...}]
but actually it did not removed the element from the json object
Share Improve this question edited Sep 12, 2014 at 10:20 samsu asked Sep 12, 2014 at 8:32 samsusamsu 712 gold badges2 silver badges8 bronze badges 1- if this is the jason string you may perhaps use this regex. try regex101./r/mA1uE2/2 – pushpraj Commented Sep 12, 2014 at 8:42
2 Answers
Reset to default 3to 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
First, items
is a property of data.items
, so to get it, you must access data.items.items
Second, it is not splice()
you want to use, but shift()
var data = {};
data.items = {"username":"usr1","profile": "usr2", "items":[{"s": "1","f": "usr2","m": "hey ebteween how u doing","fr":"usr1"}]};
data.items.items.shift();
Hope this helps
本文标签: javascriptRemove First element of JSONStack Overflow
版权声明:本文标题:javascript - Remove First element of JSON - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742175824a2427555.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论