0

I have this data:

{"items":[{"Code":"S-B-03","Quantity":"3pcs.","Desc":"Ballpen Black","Ucost":4.75},{"Code":"S-B-34","Quantity":"2pcs.","Desc":"Ballpen Black","Ucost":8.75}]}

I would like to remove the first data. I can access the first data using object.items[0]. I want to remove the first data.

I tried:

object.items[0].remove("Code");
object.items[0].remove("Quantity");
object.items[0].remove("Desc");
object.items[0].remove("Ucost");

But it is not working.

1
  • object.items.remove("Ucost"); Remove all object of json. Commented Dec 29, 2015 at 3:54

4 Answers 4

2

Remove like this.

object.items.splice(0,1)

Since items is an array we can simply use Array.splice to remove the element.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use Array.slice to replace the array. For example:

object.items = object.items.slice(1);

You can also use Array.splice which changes the contents of the array in place. For example:

object.items.splice(0, 1);

This deletes the first item from the array.

Comments

1

You can use the delete operator:

// Remove the property "Code" from object.items[0]
delete object.items[0]["Code"];

// Remove the item with index 0 from object.items:
delete object.items[0];

2 Comments

This will left undefined or null in the array. Have you tried it before posting?
@Braj I know that this doesn't rearrange the indices inside the array, I was more thinking about the remove property from object use case and added the second example for completion. Granted, I misinterpreted the OP's intent, tiredness, the object.items[0].remove("Code"); bits and vague terminology threw me off :) The perfect answer would be a combination of the two methods depending on what is removed.
0

We can remove the json data, using below step

object.items.splice(0,1)

because item is an array, we can easily remove json data using array.splice() method

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.