如何删除JSON文件中的项目 [英] How to delete item in JSON file

查看:348
本文介绍了如何删除JSON文件中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试删除JSON文件中的对象,但该对象无法正常工作.我将值p设置为等于包括值名称,日期和数字的整个项目.当我使用delete从json文件中删除特定项目时,它不会被删除.我想知道你们中的任何一个是否知道为什么.谢谢.

Hi Im trying to delete an object in a JSON file but it isnt working. I set the value p equal to the entire item which includes the values name, date and number. When I use delete in order to delete the specific item from the json file it doesn't get deleted. I was wondering if any of you knew why. Thanks.

这是nodejs代码

app.get('/testtwilio3', function(req,res,next){

        var fs = require('fs');
      var configFile = fs.readFileSync('./Public/patients.json');
      var config = JSON.parse(configFile);
        var p = {name: req.query.name, date: req.query.date, number: req.query.number};
        var key = p;
        console.log(p);
        delete config[key];
        console.log( config);
    });

这是来自控制器的代码

 $scope.deleteName = function($index){

  var patient = {
            name: $scope.patients[$index].name,
            date: $scope.patients[$index].date,
            number:$scope.patients[$index].number
        }
 $http.get('/testtwilio3', {params: {name: patient.name, number: patient.number, date: patient.date}}) // PASS THE DATA AS THE SECOND PARAMETER
    .success(
        function(success){
          alert("work");
            console.log(success)
        })
    .error(
        function(error){

            console.log("error" + error)
        });


   }

这是json文件

[{"name":"John","date":"12/22/2016","number":"781314454"},{"name":"Joe","date":"09/15/2016","number":"7892834640"},{"name":"Mike","date":"08/25/2016","number":"6472329224"},{"name":"Mark","date":"08/06/2016","number":"7819231279"}]

这是控制台显示的内容.第一行是我要删除的内容,它下面的行是项目数组.

Here is what the console is displaying. First line is what I want to be deleted and the lines below it is the array of items.

{ name: 'Mark', date: '08/06/2016', number: '7819231279' }

[ { name: 'John', date: '12/22/2016', number: '781314454' },
  { name: 'Nikhilesh Singh',
    date: '09/15/2016',
    number: '7892834640' },
  { name: 'Mike', date: '08/25/2016', number: '6472329224' },
  { name: 'Mark', date: '08/06/2016', number: '7819231279' } ]

这就是我希望它成为已从项目列表中删除第一行中项目的地方

This is how I want it to be where the item in the first line has been removed form the list of items

{ name: 'Mark', date: '08/06/2016', number: '7819231279' }

[ { name: 'John', date: '12/22/2016', number: '781314454' },
  { name: 'Nikhilesh Singh',
    date: '09/15/2016',
    number: '7892834640' },
  { name: 'Mike', date: '08/25/2016', number: '6472329224' } ]

推荐答案

删除运算符仅用于从 object 中删除属性.您的config变量包含对象的数组,因此delete根本不会执行您想要的操作.

The delete operator is only for deleting a property from an object. Your config variable contains an array of objects, so delete will not do what you want at all.

您需要遍历config数组并使用类似拼接以删除具有与请求中相同的namedatenumber值的数组项.

You need to iterate through the config array and use something like splice to delete the array items that have the same name, date, and number values as in the request.

这个相关问题可能是有帮助的.

这篇关于如何删除JSON文件中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆