Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

180
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!