Home > OS >  how to remove object from the JSON file?
how to remove object from the JSON file?

Time:03-29

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. 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:

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

TypeError: mycodes.indexOf is not a function

.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]; delete mycodes[randomcode]; but nothing is being done in this case, as it never gets removed from the file.

CodePudding user response:

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.

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'
);
  • Related