1

I have a json file and I'm trying to remove an element with below code .

 exports.delete = function(req, res) {
      var deletearticle = articles[req.params.id];
      delete articles[req.params.id];
      fs.writeFile('server/articles.json',JSON.stringify(articles, null, 4),finished);
          function finished(err){
                 console.log("--->After deletion, article list:\n" + JSON.stringify(articles, null, 4) );
                                }
     res.end(JSON.stringify(deletearticle, null, 4));
};

after removing the element my json file will be like :

[
    {
        "header": "Jack",
        "body": "Davis",
        "date": "",
        "id": 0
    },
    null,
    {
        "header": "dfd",
        "body": "dgfgfgfgfdgfg",
        "date": "2018-11-24T16:33:48.842Z",
        "id": 2
    }]

and I get error on client side :

  {articles ? articles.map(({ id, header ,body}) => (
          <div key={id} timeout={500} className="fade">
           <div className="article">
                      <h2 className="article_title">{header}</h2>

Because one of my JSON's element is null it says that can't read id from null.Is There any better way to remove this element from my json file ? or i can check if srticle is not null in client side.

Maryam Ghafarinia
  • 417
  • 1
  • 5
  • 21

4 Answers4

2

Just filter your object, Assume here a is your JSON array and you have to filter all object which does not have null

a = a.filter(function(obj){ if(obj != null) return obj })

Now print a, It will filter out all object except null objects

Naveen Ramawat
  • 1,425
  • 1
  • 15
  • 27
1

You can delete element without keeping null like this...

 array.splice(index, 1);

Or,You can remove null elements by doing like this...

var array = [
    {
        "header": "Jack",
        "body": "Davis",
        "date": "",
        "id": 0
    },
    null,
    {
        "header": "dfd",
        "body": "dgfgfgfgfdgfg",
        "date": "2018-11-24T16:33:48.842Z",
        "id": 2
    }];

// filter array

var filtered = array.filter(function (el) {
  return el != null;
});
dilusha_dasanayaka
  • 1,401
  • 2
  • 17
  • 30
1
    var a = [
        {
            "header": "Jack",
            "body": "Davis",
            "date": "",
            "id": 0
        },
        null,
        {
            "header": "dfd",
            "body": "dgfgfgfgfdgfg",
            "date": "2018-11-24T16:33:48.842Z",
            "id": 2
        }];

   //using es6 array.filter to filter null or undefined or empty object

    var filtered = array.filter((x)=>{ 
      for (var key in filter) {
        if (x[key] === undefined || x[key] !== null || x[key]!=='')
          return false;
     }
     return true;
   });
Vaibhav
  • 1,481
  • 13
  • 17
1

You can use array.splice to remove object from an array, as you have id of an object, you first need to find the index of an object to be removed and then you can remove it easily using splice.

For your reference avoid using delete

var sampleArray= [{
    "header": "Jack",
    "body": "Davis",
    "date": "",
    "id": 5
  },
  {
    "header": "Jack",
    "body": "Davis",
    "date": "",
    "id": 6
  },
  
  {
    "header": "dfd",
    "body": "dgfgfgfgfdgfg",
    "date": "2018-11-24T16:33:48.842Z",
    "id": 7
  }
];

var id = 5;

var index = sampleArray.findIndex(a=> a.id === id);
if (index > -1) {
  sampleArray.splice(index, 1);
}

console.log(sampleArray);
Just code
  • 13,553
  • 10
  • 51
  • 93