0

I have tried to many ways , but i am stuck with a simple function in javascript, and i don't know where i need to looking for ... the problem is this:

I have a Json file like this one:

{
  "blacklist": [
    {
      "email": "[email protected]"
    },
    {
      "email": "[email protected]"
    },
    {
      "email": "[email protected]"
    },
    {
      "email": "[email protected]"
    },
    {
      "email": "[email protected]"
    }
  ]
}

I would like simple remove an email with a simple function like this one:

 function cancel(email) // parameter that contain the value to delete
{
  let rawdata = fs.readFileSync('pvt.json'); //get local json file
  let mydata = JSON.parse(rawdata); //parsing rawdata 

  var key = email; //setting up key
  delete mydata.blacklist[key]; //using delete function for delete an element

  let data = JSON.stringify(mydata, null, 2); //stringify the result
  fs.writeFileSync('pvt.json', data); // overwrite local file with new one with all changes
}

the problem is ... it doesn't works ... i don't know why ... i tried to read the documentation, but i didn't found any solution 😢

4
  • What's the value of your email variable? It appears out of nowhere in the code. Is it a string containing the email address you want to remove? Commented May 2, 2020 at 14:39
  • what is this ? var key = email; //setting up key Commented May 2, 2020 at 14:39
  • @T.J. Thank you for the reply! Yes, email is a string var that contain the email to delete. Sorry, i will istant update the code ! Commented May 2, 2020 at 14:41
  • @ChandraShekhar key is the vaule to delete, email is the parameter passed in the function that contain the value to delete Commented May 2, 2020 at 14:42

2 Answers 2

1

The delete operator is for removing a property from an object, using the property's name. You're trying to remove an entry from an array, using the value of a property of an object in the array.

Assuming email is a variable containing the email address in the entry you want to remove, filter is one easy way to do that:

mydata.blacklist = mydata.blacklist.filter(entry => entry.email !== email);

filter builds a new array from the entries in the original array that meet the criteria in the callback — in this case, that their email property doesn't match the email address you want to remove.

If you wanted to modify the array in place rather than creating a new one, you'd use findIndex and splice:

const index = mydata.blacklist.findIndex(entry => entry.email === email);
if (index !== -1) {
    mydata.blacklist.splice(index, 1); // Remove the entry at the index
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you for the reply! yes, email is the value passed as parameter that we need to remove ... and yes, i already used filter, but i don't understand how to delete the value after filtered
@Zenek - You don't have to delete it after you've filtered it out. Filtering it out is, effectively, deleting it.
TypeError: mydata.findIndex is not a function
@Zenek - Sorry, should have been mydata.blacklist.
Thank you for the reply. I tried to use your function, but if i can, i would like understand what did you used (i don't like copy/paste). Thank to your function we will obtain a new array called "mydata.blacklist" that doesn't have the email searched in ?
|
0

Delete works to delete a key-value from an object. Here you have an array of items[objects]. You should use filter to remove unwanted element.

Update:

function cancel(selectedEmail) {
  let rawdata = fs.readFileSync("pvt.json"); //get local json file
  let mydata = JSON.parse(rawdata); //parsing rawdata
  mydata.blacklist = mydata.blacklist.filter(
    (item) => item.email !== selectedEmail.email
  );
  fs.writeFileSync("pvt.json", JSON.stringify(mydata, null, 2)); // overwrite local file with new one with all changes
}

Sample:

const info = {
  blacklist: [
    {
      email: "[email protected]",
    },
    {
      email: "[email protected]",
    },
    {
      email: "[email protected]",
    },
    {
      email: "[email protected]",
    },
    {
      email: "[email protected]",
    },
  ],
};
const selectedEmail = {email: "[email protected]"}

info.blacklist = info.blacklist.filter(item => item.email !== selectedEmail.email)
console.log(info)

4 Comments

Thank you for the reply. I tried this one , but it doesn't works. Email rest in the array and in the json file :/
can u see what is coming in log..email. If it is not object.. then use info.blacklist.filter(item => item.email !== email)
probably is my fault... just let me understand what you did : i have added the function code that you wrote in my js server-side script. Then you have wrote a "sample" of your code, but it doesn't have the cancel function linked in to (?) so... (?)
sample is for u to run and see output working. kind of logic to understand. U write ur own server code.

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.