删除匹配字符串的 json 数组对象项

removing a json array object item if it matches a string

在我的索引文件中,我正在使用 fs 访问这个 json。我正在尝试删除我的外部 index.js 文件中输入的字符串与“ChannelName”键的值匹配的数组对象项之一。

我曾尝试查找频道名称与字符串输入匹配的项目的索引,但这没有用(已转换为数组以尝试此操作)。

有人有什么想法吗?这甚至可能吗?

{
    "DiscordServerId":"",
    "token":"",
    "twitch_clientID":"",
    "twitch_secret":"",
    "cron":"*/10 * * * *",
    "channelID":"",
    "roleID":"everyone",
    "channels":[
        {
            "ChannelName":"channel1",
            "DiscordServer":"",
            "twitch_stream_id":"",
            "discord_nessage_id":""},
        {
            "ChannelName":"channel2",
            "DiscordServer":"",
            "twitch_stream_id":"",
            "discord_nessage_id":""
        },
        {
            "ChannelName":"channel2",
            "DiscordServer":"",
            "twitch_stream_id":"",
            "discord_nessage_id":""
        }
    ],
    "authToken":""}

您可以根据某些条件更新 channels

const searchChannelName = "channel2";
const data = {
  "DiscordServerId": "",
  "token": "",
  "twitch_clientID": "",
  "twitch_secret": "",
  "cron": "*/10 * * * *",
  "channelID": "",
  "roleID": "everyone",
  "channels": [
    {
      "ChannelName": "channel1",
      "DiscordServer": "",
      "twitch_stream_id": "",
      "discord_nessage_id": ""
    },
    {
      "ChannelName": "channel2",
      "DiscordServer": "",
      "twitch_stream_id": "",
      "discord_nessage_id": ""
    },
    {
      "ChannelName": "channel2",
      "DiscordServer": "",
      "twitch_stream_id": "",
      "discord_nessage_id": ""
    }
  ],
  "authToken": ""
}
data.channels = data.channels.filter((node => node.ChannelName !== searchChannelName));
console.log(data);