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

203
Visualizações
How to check if a value is equal to exactly one value from an array

I have the following array:

const person = [
      { name: "John", surname: "Doe" },
      { name: "Jane", surname: "Williams" }];

I want to check if a match is equal to just one name from the array. I did the following:

match === person[0].name || match === person[1].name || match === person[3].name ? "Do something" : "Blank"

I'm looking for a better way to iterate through this array, because I don't know array length.

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

0

person.forEach(function(item){
if(match === item.name){
// your logic goes here
}
})

this iterates through every object("{}") in your array and executes a function (you could use an arrow function too). The function then checks if your "match" value is equal to the "name" key in every object(referenced by "item") in the array it had ran through.

There are other array methods you can check out.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use Array.prototype.find to find in an array of objects.

The find() method returns the value of the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.

Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

As a demonstration, I have written a search persons by name form:

const persons = [
  { name: "John", surname: "Doe" },
  { name: "Jane", surname: "Williams" }
]

function bindEvent() {
  const form = document.forms['find-by-name-form']
  form.addEventListener('submit', onSubmit, true)
}

function onSubmit(event) {
  event.preventDefault()
  const { name } = event.target
  const person = findPersonByName(name.value)
  alert(person ? `${person.name} ${person.surname}` : 'no result')
  person && console.log(person)
}

function findPersonByName(name) {
  return persons.find((item) => item.name === name)  
}

window.onload = bindEvent
<form name="find-by-name-form">
  <label for="name">Find by name:</label>
  <input name="name" />
  <button>Search person</button>
</form>

about 4 years ago · Juan Pablo Isaza Relatório

0

Best solution to find index of array of match value const name = 'sandeep'

const match = persons.findindex

((person) => person.name === name)

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