Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use array .filter() to search key value in JSON array of objects

If I have an array of objects like so in playlists.json

"playlists" : [
      {
        "id" : "1",
        "owner_id" : "2",
        "song_ids" : [
          "8",
          "32"
        ]
      },
      {
        "id" : "2",
        "owner_id" : "3",
        "song_ids" : [
          "6",
          "8",
          "11"
        ]
      },
      {
        "id" : "3",
        "owner_id" : "7",
        "song_ids" : [
          "7",
          "12",
          "13",
          "16",
          "2"
        ]
      }
    ]

and in Node I read in the file like this:

const data = JSON.parse(fs.readFileSync('playlists.json'));

I want to write a .filter() function that can search by id and mutate the result into a new array thereby removing the entry tested for.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Of course we can access the index like this: playlists[0].id);

How would write the .filter() to test for a certain id i.e. generate a new array with value removed? (mutate) I wrote some code below but it is wrong and noob.

const someId = "2"
const result = playlists.filter(playlist => playlist.id !== someId)

New array of result would contain:

"playlists" : [
      {
        "id" : "1",
        "owner_id" : "2",
        "song_ids" : [
          "8",
          "32"
        ]
      },
      {
        "id" : "3",
        "owner_id" : "7",
        "song_ids" : [
          "7",
          "12",
          "13",
          "16",
          "2"
        ]
      }
    ]

>Solution :

You used filter fine, however, you have select the key from your json object.
Below is an example.

 const jsonFile = {
        "playlists" : [
              {
                "id" : "1",
                "owner_id" : "2",
                "song_ids" : [
                  "8",
                  "32"
                ]
              },
              {
                "id" : "2",
                "owner_id" : "3",
                "song_ids" : [
                  "6",
                  "8",
                  "11"
                ]
              },
              {
                "id" : "3",
                "owner_id" : "7",
                "song_ids" : [
                  "7",
                  "12",
                  "13",
                  "16",
                  "2"
                ]
              }
            ]
        }
        const someId = 2
        const result = jsonFile.playlists.filter(playlist => playlist.id !== someId)
        console.log(result)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading