简体   繁体   English

删除/更新 node.js 中的 JSON 键

[英]Remove/Update JSON key in node.js

I am trying to either update or remove (and then rewrite) a JSON key in node.js我正在尝试更新或删除(然后重写)node.js 中的 JSON 密钥

JSON: JSON:

{"users":[{"Discordid":"discid","Username":"user","Password":"pass","School":"schoolname"}, {"Discordid":"discid1","Username":"user1","Password":"pass1","School":"schoolname1"}]}

I want to remove the entire {"Discordid":"discid","Username":"user","Password":"pass","School":"schoolname"} via a for loop so I make use of variable a which equals the number in which the data is I want to delete.我想通过for 循环删除整个{"Discordid":"discid","Username":"user","Password":"pass","School":"schoolname"}所以我使用变量a这等于我要删除的数据的数量。

I have tried:我努力了:

fs.readFile('databases/magistercredentials.json', 'utf-8', function (err, data1) {
     if (err) throw err
     var magistercreds = JSON.parse(data1)
     for (a = 0; a < Object.keys(magistercreds.users).length; a++) delete magistercreds.users[a]

And other things which all did not work.和其他一切都不起作用的事情。

fs.readFile('databases/magistercredentials.json', 'utf-8', function (err, data1) {
     if (err) throw err
     var magistercreds = JSON.parse(data1)
     for (a = 0; a < magistercreds.users.length; a++){
          magistercreds.users.splice( a, 1 );
          a--; // to step back, as we removed an item, and indexes are shifted
     }

But may be you want just update, so you can make it simple:但可能你只想更新,所以你可以让它变得简单:

fs.readFile('databases/magistercredentials.json', 'utf-8', function (err, data1) {
     if (err) throw err
     var magistercreds = JSON.parse(data1)
     magistercreds.users[68468] = {.....}

The question isn't clear on whether a key is to be removed or an entire object.关于是要删除密钥还是要删除整个 object,问题尚不清楚。 Assuming an entire element is to be removed from the array users , the splice method can be used.假设要从数组users中删除整个元素,可以使用splice 方法

First find the index of the element you want to remove using findIndex .首先使用findIndex找到要删除的元素的索引。 Then you can use splice to modify the array in-place.然后,您可以使用splice就地修改数组。

Sample:样本:

fs.readFile('databases/magistercredentials.json', 'utf-8', function (err, data1) {
     if (err) throw err
     var magistercreds = JSON.parse(data1)
     // Assuming you want to delete the element which has the Discordid property as "discid"
     var indexOfElement = magistercreds.findIndex(el => el.Discordid === "discid")
     magistercreds.users.splice(indexOfElement, 1) // This will remove 1 element from the index "indexOfElement" 
}

To add on, using Object.keys to iterate over an array is not necessary.此外,不需要使用Object.keys来遍历数组。 The for loop in the original question can be rewritten as:原始问题中的 for 循环可以重写为:

for (a = 0; a < magistercreds.users.length; a++) delete magistercreds.users[a]

Please edit the question to add more information if this is not what you were looking to achieve.如果这不是您想要实现的,请编辑问题以添加更多信息。

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

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