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

182
Visualizações
checking array if has same value

Learning by coding, here i have an array of objects (data), and nodeId which is number , i want to check if that arrays 'target' has same value as nodeId then 'return', should i use map(), find(), filter(), how should i know which to use ? english is not my mother language so could be mistakes

data:

  const Test = '7'
const nodeId = parseInt(Test);

  
  const data = [
    { target: 4, name: "usa" },
    { target: 7, name: "England" },
    { target: 3, name: "Japan" }
  ];
  
  
   if (check if data and nodeId both have same value then) {
    return;
  }

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

0

.some() is probably what you want to use

const check = data.some((elem) => elem.target === nodeId) 
console.log(check) // true if exist or false if it doesnt
if(check) {
  // reached if it exist
}

check would be false if it doesn't exist or true if it does. It will stop running once it finds a match whereas .filter() and .map() would run the entire array. If you also want to get the element then you should use .find() instead.

about 4 years ago · Juan Pablo Isaza Relatório

0

Your question is a little vague but i'm going to try and answer it as easily as possible.

should i use map(), find(), filter(), how should i know which to use ?

That depends on what your hypothetical function or code would require, for example if you want the item that matched your value, then you would use .find() because it returns single value based on a condition.

similarly if you wish to return an array based on items that match the conditions, you would use .filter(), and same if you just want to check if your value exists in the array, you could use .some or .every.

in your code example, I'm assuming you want to return the specific value that matched with your nodeId, with that assumption, here's how that will work

function getDataBasedOnNodeId (dataObjects, matchValue) { 
 return dataObjects.find(item => item.target === Number(matchValue))
}

const data = [
    { target: 4, name: "usa" },
    { target: 7, name: "England" },
    { target: 3, name: "Japan" }
];
  

return getDataBasedOnNodeId(data, 7)
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