Remove json object from json array javascript

As described by @mplungjan, I though it was right. Then right away I click the up rate button. But by following it, I finally got an error.

<script>
var data = {"result":[
  {"FirstName":"Test1","LastName":"User","Email":"","City":"ahmedabad","State":"sk","Country":"canada","Status":"False","iUserID":"23"},
  {"FirstName":"user","LastName":"user","Email":"","City":"ahmedabad","State":"Gujarat","Country":"India","Status":"True","iUserID":"41"},
  {"FirstName":"Ropbert","LastName":"Jones","Email":"","City":"NewYork","State":"gfg","Country":"fgdfgdfg","Status":"True","iUserID":"48"},
  {"FirstName":"hitesh","LastName":"prajapti","Email":"","City":"","State":"","Country":"","Status":"True","iUserID":"78"}
  ]
}
alert(data.result)
delete data.result[3]
alert(data.result)
</script>

Delete is just remove the data, but the 'place' is still there as undefined.

I did this and it works like a charm :

data.result.splice(2,1);  

meaning : delete 1 item at position 3 ( because array is counted form 0, then item at no 3 is counted as no 2 )


Let’s say the following is our JSON string −

var details =
[
   {
      customerName: "Chris",
      customerAge: 32
   },
   {
      customerName: "David",
      customerAge: 26
   },
   {
      customerName: "Bob",
      customerAge: 29
   },
   {
      customerName: "Carol",
      customerAge: 25
   }
]

To remove JSON element, use the delete keyword in JavaScript.

Example

Following is the complete code to remove JSON element −

var details =
[
   {
      customerName: "Chris",
      customerAge: 32
   },
   {
      customerName: "David",
      customerAge: 26
   },
   {
      customerName: "Bob",
      customerAge: 29
   },
   {
      customerName: "Carol",
      customerAge: 25
   }
]
delete details[0].customerAge;
console.log(details);

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo211.js −

PS C:\Users\Amit\JavaScript-code> node demo211.js
[
   { customerName: 'Chris' },
   { customerName: 'David', customerAge: 26 },
   { customerName: 'Bob', customerAge: 29 },
   { customerName: 'Carol', customerAge: 25 }
]

Remove json object from json array javascript

Updated on 01-Oct-2020 13:36:31

  • Related Questions & Answers
  • Remove element by id in JavaScript?
  • How to remove a specific element from a JSON Array in Java?
  • Search by id and remove object from JSON array in JavaScript
  • JavaScript JSON Arrays
  • JavaScript JSON HTML
  • Remove element from array referencing spreaded array in JavaScript
  • Remove Element in Python
  • Convert JSON array into normal json in JavaScript
  • JavaScript JSON parse() Method
  • Remove the child node of a specific element in JavaScript?
  • How to remove every Nth element from an array JavaScript?
  • How to convert JSON text to JavaScript JSON object?
  • Convert JSON to another JSON format with recursion JavaScript
  • Remove next element using jQuery?
  • How to remove last array element in JavaScript and return it?


Suppose, we have an array of objects that contains data about some movies like this −

const arr = [
   {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"}
];

We are required to write a JavaScript function that takes in one such array as the first argument and an id string as the second argument. Then our function should search for the object by that id, and if the array contains that object, we should remove it from the array.

Example

The code for this will be −

const arr = [
   {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"}
];
const removeById = (arr, id) => {
   const requiredIndex = arr.findIndex(el => {
      return el.id === String(id);
   });
   if(requiredIndex === -1){
      return false;
   };
   return !!arr.splice(requiredIndex, 1);
};
removeById(arr, 5);
console.log(arr);

Output

And the output in the console will be −

[
   { 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: '6', name: 'Toy Story', type: 'children' }
]

Remove json object from json array javascript

Updated on 21-Nov-2020 09:55:56

  • Related Questions & Answers
  • Search a complex object by id property in JavaScript
  • Deep Search JSON Object JavaScript
  • Create array from JSON object JavaScript
  • Remove element by id in JavaScript?
  • From JSON object to an array in JavaScript
  • Retrieve user id from array of object - JavaScript
  • PHP: Remove object from array
  • Remove object from array in MongoDB?
  • JavaScript group a JSON object by two properties and count
  • Adding a unique id for each entry in JSON object in JavaScript
  • Removing property from a JSON object in JavaScript
  • Remove item from a nested array by indices in JavaScript
  • Remove json element - JavaScript?
  • Group by JavaScript Array Object
  • Build tree array from JSON in JavaScript

How do I remove an object from a JSON file?

To remove JSON element, use the delete keyword in JavaScript.

How do you remove an array from a JSON object?

You can remove an element from the JSONArray object using the remove() method. This method accepts an integer and removes the element in that particular index.

How do you remove a key value pair from a JSON object?

To remove JSON object key and value with JavaScript, we use the delete operator.

How do I delete JSON data in node JS?

How to Delete Files in Node JS.
Delete file asynchronously using Node FS unlink() function. In the below code we will try to delete a file name 'file1. ... .
Delete file synchronously using Node FS unlinkSync() function. const fs = require('fs'); ... .
Delete all files of a directory..