【问题标题】:Remove an element from JSON using NODEJS使用 NODEJS 从 JSON 中删除一个元素
【发布时间】:2018-11-26 05:16:30
【问题描述】:

我有一个 json 文件,我正在尝试使用以下代码删除一个元素。

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

删除元素后,我的 json 文件将如下所示:

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

我在客户端收到错误:

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

因为我的 JSON 元素之一是 null,所以它说无法从 null 读取 id。有没有更好的方法从我的 json 文件中删除这个元素?或者我可以检查客户端的 srticle 是否不为空。

【问题讨论】:

  • 您可以使用Array.prototype.splice将其完全删除。
  • 尝试使用 Array..prototype.splice(),确保您知道文章数组上的正确索引。参考:link
  • 我已经阅读了该页面。但我没有它的索引。
  • 用 lodash link查找索引
  • @MaryQafarinia 那么你需要找到它的索引然后splice它。

标签: arrays node.js json api


【解决方案1】:

只需过滤您的对象,假设这里 a 是您的 JSON 数组,您必须过滤所有不包含 null 的对象

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

现在打印一个,它将过滤掉除空对象之外的所有对象

【讨论】:

    【解决方案2】:

    您可以使用array.splice从数组中删除对象,因为您有一个对象的id,您首先需要找到要删除的对象的索引,然后您可以使用拼接轻松地将其删除。

    供您参考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);

    【讨论】:

      【解决方案3】:

      你可以像这样删除元素而不保持 null ......

       array.splice(index, 1);
      

      或者,您可以像这样删除空元素...

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

      【讨论】:

        【解决方案4】:
            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;
           });
        

        【讨论】:

          猜你喜欢
          • 2023-03-22
          • 1970-01-01
          • 1970-01-01
          • 2020-02-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-10-03
          • 2020-10-05
          相关资源
          最近更新 更多