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

137
Visualizações
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 Respostas
Responde à pergunta

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 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