Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

135
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda