ThisisD
ThisisD

Reputation: 17

Remove data array JS

I am working on creating a file where data can be read from a JSON file.

I can add new names to the file but I am unable to delete. When I enter a name to delete it actually adds the name to the file.

Why is it adding & not deleting? The objective is to be able to delete a specific name from the list that will be generated.

Thank you in advance! Here is my code with comments on what I am attempting to do.

// POST request to add to JSON & XML files
router.post('/post/json', function(req, res) {

    // Function to read in a JSON file, add to it & convert to XML
    function appendJSON(obj) {

        // Read in a JSON file
        var JSONfile = fs.readFileSync('Staff.json', 'utf8');

        // Parse the JSON file in order to be able to edit it 
        var JSONparsed = JSON.parse(JSONfile);

        // Add a new record into country array within the JSON file    
        JSONparsed.member.push(obj);

        // Beautify the resulting JSON file
        var JSONformated = JSON.stringify(JSONparsed, null, 4);

        // Delte a specific entry from JSON file
        var i = member.indexOf(" ");
        if (i != -1) {
            member.splice(i,1);
        }

        // Write the updated JSON file back to the system 
        fs.writeFileSync('Staff.json', JSONformated);

        // Convert the updated JSON file to XML     
        var XMLformated = js2xmlparser.parse('staff', JSON.parse(JSONformated));

        // Write the resulting XML back to the system
        fs.writeFileSync('Staff.xml', XMLformated);
    }

    // Call appendJSON function and pass in body of the current POST request
    appendJSON(req.body);

    // Re-direct the browser back to the page, where the POST request came from
    res.redirect('back');
});

Here is an example of the JSON file

{ 
    "member": [ 
        { 
            "Full_Name": "", 
            "Address": "", 
            "Gender": "",     
            "Phone_Number": "" 
        }
    ]
}

Upvotes: 1

Views: 204

Answers (1)

NtFreX
NtFreX

Reputation: 11377

The splice function removes the item from the array and returns the deleted item. So if you want to delete an item by an attribute from your JSON like Full_Name you have to find the index of the item first.

var nameToSearch = "MyName";
var itemIndex = -1;
for(var i = 0; i < JSONparsed.member.length; i++) {
    if(JSONparsed.member[i].Full_Name === nameToSearch) {
        itemIndex = i;
    }
}

And then you can delete the item like you did.

if (itemIndex != -1) {
    JSONparsed.member.splice(itemIndex,1);
}

The problem you most likely had was that the itemIndex was allways -1 as indexOf did not know which attribute to check and just checks for the whole object.

Also the following lines have to be after the code above. (So after any changes you make to the json)

// Beautify the resulting JSON file
var JSONformated = JSON.stringify(JSONparsed, null, 4);

I also recomend reading this turtorial about javascript debugging. It makes your live a lot easyier to find mistakes.

Upvotes: 1

Related Questions