简体   繁体   English

如何从 JSON 文件中删除 object?

[英]how to remove object from the JSON file?

I'm unable to remove random code generated from the JSON array object, .splice worked very fine for me while removing the whole line with all data.我无法删除从 JSON 数组 object 生成的随机代码, .splice在删除包含所有数据的整行时对我来说工作得很好。 I have different tiers of codes, in this example I will need to remove just the random generated value from the "promoCodes" I will always receive this error:我有不同层次的代码,在这个例子中我只需要从“promoCodes”中删除随机生成的值我总是会收到这个错误:

mycodes.slice(mycodes.indexOf(randomcode), 1)

TypeError: mycodes.indexOf is not a function

.JSON .JSON

{
    "TIER1": {
        "rewardSize": 1,
        "probability": 0.8,
        "numberOfCodes": 2,
        "rewardSubtype": 1,
        "totalRewardSize": 2,
        "promoCodes": [
            "TEST1 - 1",
            "TEST1 - 2"
        ]
      },
      "TIER2": {
        "rewardSize": 3,
        "probability": 0.25,
        "numberOfCodes": 2,
        "rewardSubtype": 2,
        "totalRewardSize": 6,
        "promoCodes": [
            "TEST2 - 1",
            "TEST2 - 2"
        ]
      },
      "TIER3": {
        "rewardSize": 10,
        "probability": 0.15,
        "numberOfCodes": 2,
        "rewardSubtype": 3,
        "totalRewardSize": 20,
        "promoCodes": [
            "TEST3 - 1",
            "TEST3 - 2"
        ]
      },
      "TIER4": {
        "rewardSize": 25,
        "probability": 0.05,
        "numberOfCodes": 2,
        "rewardSubtype": 4,
        "totalRewardSize": 50,
        "promoCodes": [
            "TEST4 - 1",
            "TEST4 - 2"
        ]
      },
      "TIER5": {
        "rewardSize": 50,
        "probability": 0.04,
        "numberOfCodes": 2,
        "rewardSubtype": 5,
        "totalRewardSize": 100,
        "promoCodes": [
            "TEST5 - 1",
            "TEST5 - 2"
        ]
      },
      "TIER6": {
        "rewardSize": 100,
        "probability": 0.01,
        "numberOfCodes": 2,
        "rewardSubtype": 6,
        "totalRewardSize": 200,
        "promoCodes": [
            "TEST6 - 1",
            "TEST6 - 2"
        ]
      },
      "TIER7": {
        "rewardSize": 1000,
        "probability": 0.001,
        "numberOfCodes": 2,
        "totalRewardSize": 2000,
        "promoCodes": [
            "TEST7 - 1",
            "TEST7 - 2"
        ]
      }
    }

Code:代码:

//fs setup
const fs = require('fs');
let rawdata = fs.readFileSync(__dirname + '/newtest.json', 'utf8');
let mycodes = JSON.parse(rawdata);
const TIER1 = mycodes['TIER1']['promoCodes'] 


//something above...
const randomcode = TIER1[Math.floor(Math.random() * TIER1.length)];
console.log(randomcode); //logs random generated code 
mycodes.slice(mycodes.indexOf(randomcode), 1)
fs.writeFileSync(__dirname + '/newtest.json', JSON.stringify(mycodes, 0, 4), 'utf8')

With delete I tried: delete [randomcode];我试过deletedelete [randomcode]; delete mycodes[randomcode]; but nothing is being done in this case, as it never gets removed from the file.但在这种情况下什么也没做,因为它永远不会从文件中删除。

The problem is you're trying to call the slice method on an object - the mycodes object - and this not gonna happen, because, the slice method is only defined for String or Array Standard built-in objects.问题是你试图在object上调用slice方法 - mycodes object - 这不会发生,因为slice方法仅为StringArray Standard 内置对象定义。

One Solution一个解决方案

const fs = require('fs');
let rawdata = fs.readFileSync(__dirname + '/newtest.json', 'utf8');
let mycodes = JSON.parse(rawdata);
const TIER1 = mycodes.TIER1.promoCodes;
const randomcode = TIER1[Math.floor(Math.random() * TIER1.length)];

// get the item from the TIER1 array that matches the randomcode
const foundItem = TIER1.find((item) => item === randomcode);

// remove that item from the TIER1 array
TIER1.splice(TIER1.indexOf(foundItem), 1);

// save the data again to the json file.
fs.writeFileSync(
    __dirname + '/newtest.json',
    JSON.stringify(mycodes, 0, 4),
    'utf-8'
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM