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

204
Views
Checking against an object property in an array javascript
var array = [];

Now, I am creating an object with post requests.

var object = {
  property 1: req.body.x;
  property 2: req.body.y
};

if the property 1 value(i.e. req.body.x) already exists in any of the objects in the array, it should giving a warning. Else we need to do array.push(object).

How to check for the property 1 value every time with the post request.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use array.Find in order to check if an object exists in the array with the same property.

const arr = [{
  prop1: 'foo', // prop1 of `foo` already in array
}]

// small function so we can avoid code duplication
const existsInArray = (obj) => {
  return arr.find((a) => a.prop1 === obj.prop1)
}

const obj1 = {
  prop1: 'foo',
}

const obj2 = {
  prop1: 'bar',
}

if (existsInArray(obj1)) {
  console.warn('obj1 already in array')
} else {
  arr.push(obj1)
}

if (existsInArray(obj2)) {
  console.warn('obj2 already in array')
} else {
  arr.push(obj2)
}

console.log(arr)

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!