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

134
Views
how to retrieve the object from array of objects and arrays using javascript

I have array of objects and arrays.

If the array of objects has value property same

  • and if place includes of arrraylist list return first obj

  • and if place is equal includes/not includes return first obj

if no above conditions return undefined; using javascript

var list=['SG','TH','MY']


var arrobj1=[
  {id:1, name:'userone',place:'SG', value:100},
  {id:2, name:'usertwo',place:'TH', value:100},
  {id:3, name:'userthree',place:'IL',value:200},
]
Expected Output
{id:1, name:'userone',place:'SG', value:100}

****
var arrobj2=[
  {id:1, name:'userone',place:'IN', value: 200},
  {id:2, name:'usertwo',place:'SL',value: 100},
  {id:3, name:'userthree',place:'SL', value: 100},
]
Expected Output
{id:2, name:'usertwo',place:'SL',value: 100}
****
var arrobj3=[
  {id:1, name:'userone',place:'SL', value:10},
  {id:2, name:'usertwo',place:'IN', value:20},
  {id:3, name:'userthree',place:'KL', value:30},
]
Expected Output
undefined

Tried

var result= arrobj.find(e=>{
  if((e.value === e.value) && (list.includes(e.place)){
   return e
  }
})


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

0

I've added inline comments to correspond to the requirements. I wasn't sure exactly what you meant in some cases, so I can adjust the answer if you clarify the question

var list = ['SG', 'TH', 'MY']


var arrobj1 = [{
    id: 1,
    name: 'userone',
    place: 'SG',
    value: 100
  },
  {
    id: 2,
    name: 'usertwo',
    place: 'TH',
    value: 100
  },
  {
    id: 3,
    name: 'userthree',
    place: 'IL',
    value: 200
  },
]
//Expected Output
//{id:1, name:'userone',place:'SG', value:100}

//****
var arrobj2 = [{
    id: 1,
    name: 'userone',
    place: 'IN',
    value: 200
  },
  {
    id: 2,
    name: 'usertwo',
    place: 'SL',
    value: 100
  },
  {
    id: 3,
    name: 'userthree',
    place: 'SL',
    value: 100
  },
]
//Expected Output
//{id:2, name:'usertwo',place:'SL',value: 100}
//****
var arrobj3 = [{
    id: 1,
    name: 'userone',
    place: 'SL',
    value: 10
  },
  {
    id: 2,
    name: 'usertwo',
    place: 'IN',
    value: 20
  },
  {
    id: 3,
    name: 'userthree',
    place: 'KL',
    value: 30
  },
]
//Expected Output
//undefined

function getMatch(places, arrObj) {
  // .find will return the first item that matches the condition provided
  return arrObj.find(
    l => list.includes(l.place) // either the place of this item must exist in the list
|| arrObj.filter(arr => arr.value === l.value).length > 1) // or the value of this item must appear more than once in the list
}


console.log(getMatch(list, arrobj1));
console.log(getMatch(list, arrobj2));
console.log(getMatch(list, arrobj3));

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!