Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

248
Vistas
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.

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"
        ]
      }
    ]
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

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)
about 4 years ago · Juan Pablo Isaza Denunciar

0

After some inspection. I needed to used the parent object reference with playlists to achieve the proper result like so:

const someId = "2";
const result = data.playlists.filter(playlist => playlist.id !== someId)
console.log(result);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda