如何从 JSON 文件中删除对象?
how to remove object from the JSON file?
我无法删除从 JSON 数组对象生成的随机代码,.splice
在删除包含所有数据的整行时对我来说工作得很好。我有不同的代码层,在这个例子中我只需要从“promoCodes”中删除随机生成的值我将始终收到此错误:
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"
]
}
}
代码:
//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')
使用 delete
我试过:
delete [randomcode];
delete mycodes[randomcode];
但在这种情况下什么也没做,因为它永远不会从文件中删除。
问题是您正试图在 object
- mycodes
对象上调用 slice
方法 - 这不会发生,因为 slice
方法仅为 String
或 Array
标准 built-in 对象定义。
一个解决方案
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'
);
我无法删除从 JSON 数组对象生成的随机代码,.splice
在删除包含所有数据的整行时对我来说工作得很好。我有不同的代码层,在这个例子中我只需要从“promoCodes”中删除随机生成的值我将始终收到此错误:
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"
]
}
}
代码:
//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')
使用 delete
我试过:
delete [randomcode];
delete mycodes[randomcode];
但在这种情况下什么也没做,因为它永远不会从文件中删除。
问题是您正试图在 object
- mycodes
对象上调用 slice
方法 - 这不会发生,因为 slice
方法仅为 String
或 Array
标准 built-in 对象定义。
一个解决方案
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'
);