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.

7
  • 3
    you can remove it completely using Array.prototype.splice. Commented Nov 26, 2018 at 5:21
  • try to use Array..prototype.splice(), make sure you know the correct index on the articles array. ref: link Commented Nov 26, 2018 at 5:23
  • I've been read that page.but I don't have its index. Commented Nov 26, 2018 at 5:23
  • find index with lodash link Commented Nov 26, 2018 at 5:25
  • @MaryQafarinia then you would need to find its index then splice it. Commented Nov 26, 2018 at 5:26

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

Comments

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);

Comments

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;
});

Comments

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;
   });

Comments

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.