Home > front end >  removing a json array object item if it matches a string
removing a json array object item if it matches a string

Time:09-21

In my index file, I am accessing this json using fs. I am trying to remove one of the array object items where a string input in my external index.js file matches the value of the "ChannelName" key.

I have tried finding the index of the item where the channelname matches the string input but this didn't work(Converted to an array to try this).

Has anyone any ideas? Is this even possible?

{
    "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":""}

CodePudding user response:

You can update the channels with some condition.

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);

  • Related