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

154
Visualizações
How to find names by id

How to find names by id's ? I have two arrays and want to find names by their id's.

person: [
  {id: 1, name: 'abc'},
  {id: 2, name: 'xyz'},
  {id: 3, name: 'pqr'},
  ]

data: [
  {id: 1, personId: [1,2,3]},
  {id: 2, personId: [1,3]},
  {id: 3, personId: [1,2]},
]

Expected Output :

personId: [1,2,3] return // abc,xyz,pqr
personId: [1,3] return // abc,pqr
personId: [1,2] return // abc,xyz

I am using react-native. I have tried this :

for (let person of this.state.data) {
  for (let personName of person['personId']){
    let name = this.state.person.find(nme => nme['id'] === personName);
    alert(name);
  }  
}

Any help would be greatly appreciated

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can use find

result = []
for (let i=0; i < personId.length; i++) {
    result.push(person.find(data => data.id === personId[i]).name);
}

in order to be able to retrieve objects in your person array based on the id.

about 4 years ago · Juan Pablo Isaza Relatório

0

I guess that's JSON data stringify-ied or a JSON in general. So, you can first parse it to JS object to make it easier for you to use JS. But let me leave that part to you. I changed your data to array so that I can directly show you what I would do if it helps. It's good to use methods that don't mutate original data. So I used array methods slice, forEach and map in this case. This way you can safely create a new data that is a replica of data (var data) but by including new property 'personNames', which is mapped from the other variable (person).

const person = [
  {id: 1, name: 'abc'},
  {id: 2, name: 'xyz'},
  {id: 3, name: 'pqr'},
];
const data = [
  {id: 1, personId: [1,2,3]},
  {id: 2, personId: [1,3]},
  {id: 3, personId: [1,2]},
];

// not to mutate your original data, just in case
const dataWithNames = data.slice();
// console.log(dataWithNames);

// modifying new data to include personNames property
dataWithNames.forEach( eachData => {
  // let eachIdP = eachData.personId;
  // creating new array by mapping person id to names from person data
  let mappedNames = eachData.personId.map( pID => {
      // goes to refer person data with same id
      person.forEach( eachPerson => {
         if ( eachPerson.id === pID ) {
              pID = eachPerson.name;
         }
      } );
      return pID;
  } );
  // console.log(mappedNames);

  // adding new property 'personNames' and 
  // assign them to respective mappedNames
  eachData.personNames = mappedNames;
} );
// console.log(dataWithNames);

This will get you the following.

/*
[
 {
  id: 1,
  personId: [ 1, 2, 3 ],
  personNames: [ 'abc', 'xyz', 'pqr' ]
 },
 { id: 2, personId: [ 1, 3 ], personNames: [ 'abc', 'pqr' ] },
 { id: 3, personId: [ 1, 2 ], personNames: [ 'abc', 'xyz' ] }
]*/

I hope this will help you.

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