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
Add a ment  | 

2 Answers 2

Reset to default 3

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

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