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. Show
Delete is just remove the data, but the 'place' is still there as undefined. I did this and it works like a charm :
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. ExampleFollowing 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 } ]
Updated on 01-Oct-2020 13:36:31
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. ExampleThe 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); OutputAnd 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' } ]
Updated on 21-Nov-2020 09:55:56
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.. |