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

132
Visualizações
How do i filter a value of key in an object based on matching value?

I have an object that contains objects. How do i filter out the value of "title" based on whether the id is matching?

{
    "0": {
        "id": 1912,
        "title": "Fiction",
    },
    "1": {
        "id": 1957,
        "title": "Non-Fiction",
    },
    "2": {
        "id": 1958,
        "title": "Literature",
    },
}

For example, if the const id=1958, then the output: Literature

Output:

Literature
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can try this:

let data = {
    "0": {
        "id": 1912,
        "title": "Fiction",
    },
    "1": {
        "id": 1957,
        "title": "Non-Fiction",
    },
    "2": {
        "id": 1958,
        "title": "Literature",
    },
};

let getTitle = (obj, id)=>{
 return Object.values(obj).filter(x=>x.id===id)?.[0]?.title
};

console.log(getTitle(data,1958))

You can also use find instead of filter.

Without optional chaining as per OPs comment:

   let data = {
        "0": {
            "id": 1912,
            "title": "Fiction",
        },
        "1": {
            "id": 1957,
            "title": "Non-Fiction",
        },
        "2": {
            "id": 1958,
            "title": "Literature",
        },
    };

    let getTitle = (obj, id)=>{
      let element = Object.values(obj).find(x=>x.id===id);
      return element ? element.title : "";
    };

    console.log(getTitle(data,1958))

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