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

158
Visualizações
Print object that matches value in an array

I have an the following array of items:

[
{
  url: "www.google.com",
  name: "name1"
},
{
  url: "www.google.com2",
  name: "name1"
},
{
  url: "www.google.com3",
  name: "name1"
}
]

and a variable:

let url = "www.google.com2"

How would I check if the variable exists in the array, and if it exists, how would I print the specific object that marches the variable?

I am able to check if the variable exists in the array, but how would I print the specific object that has the same url as the variable?

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

0

  • you can loop though an array via forEach method
  • and add if condition to check if url is matching and inside condition console log entire(found) object

const input = [
{
url: "www.google.com",
name: "name1"
},
{
url: "www.google.com2",
name: "name1"
},
{
url: "www.google.com3",
name: "name1"
}
]


let url = "www.google.com2"

input.forEach(object => {
  if(object.url === url) {
  console.log(object)
  }
})

  • or if would like to find only one element in the array

  • you can use find method

  • also you can store found element in the variable

const input = [
{
url: "www.google.com",
name: "name1"
},
{
url: "www.google.com2",
name: "name1"
},
{
url: "www.google.com3",
name: "name1"
}
]


let url = "www.google.com2"

const foundObject = input.find(object => object.url === url)

console.log('foundObject', foundObject)

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use JS filter()function for that. Filter fives you all matches. If you sure that your input array contains only once this url then you can use: r[0] ?? null; Then you will get only the object.

const input = [
  {
  url: "www.google.com",
  name: "name1"
  },
  {
  url: "www.google.com2",
  name: "name1"
  },
  {
  url: "www.google.com3",
  name: "name1"
  }
]
const url = 'www.google.com2';
const r = input.filter(e => e.url === url);

console.log('result: ',r);
console.log('resultOne: ',r[0] ?? 'nothing found');

about 4 years ago · Juan Pablo Isaza Relatório

0

If you want to print all the objects that contain the desired url you should go with @shutsman's answer.

If you are sure that there is only one object contained with the property (incase of an id or something) you could use:

const input = [
{
url: "www.google.com",
name: "name1"
},
{
url: "www.google.com2",
name: "name1"
},
{
url: "www.google.com3",
name: "name1"
}
]


let url = "www.google.com2"

console.log(input.find(obj => obj.url === url))
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