Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

145
Visualizações
Filter json by key in Javascript

I need to filter a json which i get externally from API. The json file is like this :

{
 "get":"players",
   "parameters":{
      "team":"504",
      "season":"2021"
   },
   "response":[
      {
         "player":{
            "id":1011,
            "name":"K. R\u00fcegg",
            "firstname":"Kevin",
            "lastname":"R\u00fcegg",
           [...]
      },
      {
         "player":{
            "id":14268,
            "name":"I. Pandur",
            "firstname":"Ivor",
            "lastname":"Pandur",
            "age":21,
            "birth":{ [...]

Syntax :

      fetch(url)
            .then(response => response.json())
            .then((data) => {
               [...]
              });

I need to get only the selected player id like this :

 "player":{
            "id":1011,
            "name":"K. R\u00fcegg",
            "firstname":"Kevin",
            "lastname":"R\u00fcegg",
           [...]
      },
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I hope following example code will give you some idea..

I used JsonPlaceholder Mock API https://jsonplaceholder.typicode.

Above mentioned API giving data ouput is:

[
  {
    "userId": 1,
    "id": 1,
    "title": "delectus aut autem",
    "completed": false
  },
  {
    "userId": 1,
    "id": 2,
    "title": "quis ut nam facilis et officia qui",
    "completed": false
  },
  {
    "userId": 1,
    "id": 3,
    "title": "fugiat veniam minus",
    "completed": false
  },
  [...]

I have to separate following selected result from above mentioned API, (It's Id is 187):

{
  completed: false,
  id: 187,
  title: "consequuntur animi possimus",
  userId: 10
}

For that I have used following JS code:

var  targetID = 187; // need to filter with following ID

fetch('https://jsonplaceholder.typicode.com/todos/')
  .then(response => response.json())
  .then((data) => {
    const result = data.filter(function (el) {
      return el.id == targetID; // Changed the matching value
    });
    console.log('FIltered Value', result);
});

Working Fiddle https://jsfiddle.net/katheer_mizal/9jdh5wuy/1/

More Details:

Js parse and filter a json

Find object by id in an array of JavaScript objects

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda