代码之家  ›  专栏  ›  技术社区  ›  Anonymous

根据值从JSON对象中删除元素

  •  -2
  • Anonymous  · 技术社区  · 8 年前

    我有一个JSON文件,看起来是这样的:

    {
        "2018-1-15 22:35:22": {
            "entry": "1234",
            "ticket": "32432432432523",
            "name": "test"
        },
    
        "2018-1-15 23:35:22": {
            "entry": "5678",
            "ticket": "2485181851981",
            "name": "test2"
        }
    
    }
    

    我有一段代码来检查JSON文件中是否有条目值:

    const jsondata = require('./path/to/data.json');
    
    function _isContains(json, value) {
        let contains = false;
        Object.keys(json).some(key => {
            contains = typeof json[key] === 'object' ? _isContains(json[key], value) : json[key] === value;
            return contains;
        });
        return contains;
    }
    
    var entryToDelete = 1234
    if (_isContains(jsondata, entryToDelete) == true) {
        //delete the entire {} for the entryToDelete 
    }
    

    基本上,如果JSON文件中已经存在条目,我想删除该元素。因此,删除元素后,JSON文件应如下所示:

    {
        "2018-1-15 23:35:22": {
            "entry": "5678",
            "ticket": "2485181851981",
            "name": "test2"
        }   
    
    }
    

    我试着用 delete jsondata[entryToDelete]; ,但这并没有删除元素。

    谁能帮我修一下这个吗。 谢谢

    3 回复  |  直到 8 年前
        1
  •  1
  •   Kevin Jantzer    8 年前

    以下是您的脚本,已根据需要进行修改:

    var jsonData = {
        "2018-1-15 22:35:22": {
            "entry": "1234",
            "ticket": "32432432432523",
            "name": "test"
        },
    
        "2018-1-15 23:35:22": {
            "entry": "5678",
            "ticket": "2485181851981",
            "name": "test2"
        }
    
    }
    
    function _isContains(json, value) {
        let contains = false;
        Object.keys(json).some(key => {
            contains = typeof json[key] === 'object' ? _isContains(json[key], value) : json[key] === value;
            return contains = key;
        });
        return contains;
    }
    
    var entryToDelete = 1234
    var contains = _isContains(jsonData, entryToDelete)
    
    if ( contains !== false) {
        delete jsonData[contains]
        console.log(jsonData)
    }
        2
  •  1
  •   some    8 年前

    const jsondata = {
        "2018-1-15 22:35:22": {
            "entry": "1234",
            "ticket": "32432432432523",
            "name": "test"
        },
    
        "2018-1-15 23:35:22": {
            "entry": "5678",
            "ticket": "2485181851981",
            "name": "test2"
        }
    
    }
    
    function getKeyFromValue(json, value) {
        let output = null; // assume we don't find the entry
        Object.keys(json).some(key => {
          // if entry is equal to value, then set output to key
          if ( json[key].entry === value ) output=key;
          // return output. As long as it is null, it will continue to with next entry.
          return output;
        });
        return output; // returns the key
    }
    
    var entryToDelete = "1234"
    var key = getKeyFromValue(jsondata, entryToDelete);
    console.log('key', key);
    // if the key is set (no need to test for not null)
    if (key) delete jsondata[key];
    console.log(jsondata);
        3
  •  0
  •   Hamza Makraz    8 年前

    这就是你需要的!

    var entryToDelete = 1234
    for (var key in jsondata) {
        if (jsondata.hasOwnProperty(key) && jsondata[key].entry == entryToDelete) {
            delete jsondata[key];
        }
    }