在使用后,我無法找到洗掉整行 JSON 資料(行)的方法。由于某種原因delete不作業,或者說什么都不做。
.JSON
[
{"id":"1","code":"super-S","expires_in":"","gives_currencies":"1.5","gives_items":"","number_of_attempts":"1","attempts_used":""},
{"id":"2","code":"wow!","expires_in":"","gives_currencies":"3","gives_items":"","number_of_attempts":"1","attempts_used":""},
{"id":"3","code":"slashme","expires_in":"","gives_currencies":"4","gives_items":"","number_of_attempts":"1","attempts_used":""},
{"id":"4","code":"randombla","expires_in":"","gives_currencies":"5","gives_items":"","number_of_attempts":"1","attempts_used":""}
]
代碼
//fs configuration
const fs = require('fs');
let rawdata = fs.readFileSync('test.json');
let mycodes = JSON.parse(rawdata);
//something above
const randomcode = mycodes[Math.floor(Math.random() * mycodes.length)];
console.log('Your code is:', randomcode['code']); //logs me a random code value
delete mycodes[randomcode];
這里的目標是選擇隨機代碼,這已經完成,但是我需要將它從 .JSON 檔案中洗掉,這樣它就不會重復了。我嘗試了幾件事,但它不起作用,delete.randomcode等等......該行從未從 .JSON 檔案中洗掉。
uj5u.com熱心網友回復:
使用Array.prototype。splice (index, deleteCount) 而不是delete.
delete,在陣列上,將只是null鍵,而不洗掉它。
將修改后的資料保存JSON.stringify(mycodes)到同一個檔案中。
const fs = require('fs');
const mycodes = JSON.parse(fs.readFileSync('./test.json'));
const randomIndex = Math.floor(Math.random() * mycodes.length);
const randomObject = mycodes[randomIndex];
console.log('Your code is:', randomObject.code); // Log a random code value
mycodes.splice(randomIndex, 1); // Remove one key at randomIndex
// Write back to file
fs.writeFileSync('test.json', JSON.stringify(mycodes, 0, 4), 'utf8');
如果您已經從陣列中獲得了該物件,并且由于物件是通過參考傳遞的(例如記憶體中的指標),請使用Array.prototype.indexOf(someObject),例如:
const fs = require('fs');
const mycodes = JSON.parse(fs.readFileSync('./test.json'));
const randomIndex = Math.floor(Math.random() * mycodes.length);
const randomObject = mycodes[randomIndex];
// later in your code....
const objIndex = mycodes.indexOf(randomObject); // Get Object index in Array
mycodes.splice(objIndex, 1); // Remove it from array at that index
// Write back to file
fs.writeFileSync('test.json', JSON.stringify(mycodes, 0, 4), 'utf8');
uj5u.com熱心網友回復:
您需要在使用JSON.stringify().
當您使用它時,您可以將代碼移動到函式中,這將使其更易于閱讀和使用。
您可能還想閱讀有關使用Array.prototype.splice().
delete運算子用于從物件中洗掉屬性。雖然您可以使用它從陣列中洗掉元素,但它會將索引留空,而不是在洗掉后關閉陣列中的間隙。
const fs = require('fs');
// get a random element from any array
function getRandomElement (array) {
const randomElement = array[Math.floor(Math.random() * array.length)];
return randomElement;
}
function deleteElementFromArray (array, element) {
const index = array.indexOf(element);
if (index < 0) return false;
array.splice(index, 1);
return true;
}
// move the reading work inside a function
function readJson (filePath) {
const json = fs.readFileSync(filePath, {encoding: 'utf8'});
const data = JSON.parse(json);
return data;
}
// move the reading work inside a function
function writeJson (filePath, data) {
const json = JSON.stringify(data);
fs.writeFileSync(filePath, json);
}
const jsonFilePath = 'test.json';
const mycodes = readJson(jsonFilePath);
const randomcode = getRandomElement(mycodes);
console.log('Your code is:', randomcode['code']);
deleteElementFromArray(mycodes, randomcode);
writeJson(jsonFilePath, mycodes);
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/443515.html
標籤:javascript 节点.js json 文件 fs
上一篇:如何使用pythonpytest模擬實體化的模擬物件的回應
下一篇:節點:行程和行程之間有什么區別?
