JSONファイルからデータを読み込むことができるファイルを作成しています。データ配列を削除するJS
ファイルに新しい名前を追加できますが、削除できません。削除する名前を入力すると、実際にファイルに名前が追加されます。
&を削除しないのはなぜですか?目的は、生成されるリストから特定の名前を削除できるようにすることです。
ありがとうございます!ここで私がしようとしていることについてコメントしています。ここ
// POST request to add to JSON & XML files
router.post('/post/json', function(req, res) {
// Function to read in a JSON file, add to it & convert to XML
function appendJSON(obj) {
// Read in a JSON file
var JSONfile = fs.readFileSync('Staff.json', 'utf8');
// Parse the JSON file in order to be able to edit it
var JSONparsed = JSON.parse(JSONfile);
// Add a new record into country array within the JSON file
JSONparsed.member.push(obj);
// Beautify the resulting JSON file
var JSONformated = JSON.stringify(JSONparsed, null, 4);
// Delte a specific entry from JSON file
var i = member.indexOf(" ");
if (i != -1) {
member.splice(i,1);
}
// Write the updated JSON file back to the system
fs.writeFileSync('Staff.json', JSONformated);
// Convert the updated JSON file to XML
var XMLformated = js2xmlparser.parse('staff', JSON.parse(JSONformated));
// Write the resulting XML back to the system
fs.writeFileSync('Staff.xml', XMLformated);
}
// Call appendJSON function and pass in body of the current POST request
appendJSON(req.body);
// Re-direct the browser back to the page, where the POST request came from
res.redirect('back');
});
がJSON
ファイル
私は上記を使用するか、ここで削除方法を使用する方が良いですか? – ThisisD
私の悪い、私はいつも 'スプライス'と 'スライス'を混乱させる。 'splice'がその場所で修正されます。 –
spliceメソッドは、"削除された要素を含む配列 "を返します。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice#Return_value – baao