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

162
Visualizações
How to filter few properties of an object which is in Array of objects if one property equals property from another object

I have a object which has some properties for one user, and I have array of objects which is returned from API. My goal is to check which object of Array of objects has the same property as the one single initial object, and then it should return only part of it's properities. I have tried to use .map on Array of objects but it seems not workig.

Below is the code example. I have also prepared codesandbox if You wish.

const user = 
    {
      name: "jan",
      lastName: "kowalski",
      fullName: "jan kowalski",
      car: "audi"
    }
  ;

  const usersAnimal = [
    {
      name: "jan",
      lastName: "kowalski",
      fullName: "jan kowalski",
      animal: "cat",
      animalSize: "small",
      animalName: "Bat"
    },
    {
      name: "john",
      lastName: "smith",
      fullName: "john smith",
      animal: "dog",
      animalSize: "middle",
      animalName: "Jerry"
    },
    {
      name: "Anna",
      lastName: "Nilsson",
      fullName: "Anna Nilsson",
      animal: "cow",
      animalSize: "big",
      animalName: "Dorrie"
    }
  ];

  const filtered = usersAnimal.map((userAnimal)=>userAnimal.fullName === user.fullName && return userAnimal.animalName & userAnimal.animalSize & userAnimal.animal);

thanks

https://codesandbox.io/s/admiring-edison-qxff42?file=/src/App.js

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

For case like this, it would be far easier if you filter it out first then proceed using map:

 const filtered = usersAnimal
    .filter((animal) => animal.fullName === user.fullName)
    .map(({ animalName, animalSize, animal }) => {
      return {
        animalName,
        animalSize,
        animal
      };
    });
about 4 years ago · Santiago Trujillo Relatório

0

If you want to filter, I recommend you to use filter.

The map method will create a new array, the content of which is the set of results returned by each element of the original array after the callback function is operated

const user = {name:"jan",lastName:"kowalski",fullName:"jan kowalski",car:"audi"};

const usersAnimal = [{name:"jan",lastName:"kowalski",fullName:"jan kowalski",animal:"cat",animalSize:"small",animalName:"Bat"},{name:"john",lastName:"smith",fullName:"john smith",animal:"dog",animalSize:"middle",animalName:"Jerry"}];

// Get an array of matching objects
let filtered = 
  usersAnimal.filter(o => o.fullName === user.fullName);
  
// You get the filtered array, then you can get the required properties  
filtered.forEach(o => {
  console.log(
    'animal:%s, animalSize:%s, animalName:%s',
    o?.animal, o?.animalSize, o?.animalName
  );
});

// Then use map to process each element
filtered = filtered.map(o => {
  const {animal, animalSize, animalName} = o;
  return {animal, animalSize, animalName};
});

console.log('filtered', filtered);

about 4 years ago · Santiago Trujillo Relatório

0

I am providing a for loop solution as I haven't learnt many array methods in javascript.

For me the simplest option is to use a for loop and an if check to loop through the arrays values to check for included values.

for (let v in usersAnimal) {
    if (usersAnimal[v].fullName === user.fullName) {
        console.log(usersAnimal[v])
    }
}

The code above will log the entire usersAnimal object containing the fullname we are looking for.

{
  name: 'jan',
  lastName: 'kowalski',
  fullName: 'jan kowalski',
  animal: 'cat',
  animalSize: 'small',
  animalName: 'Bat'
}

commented for further understanding

for (let v in usersAnimal) {
//loops though the array
    if (usersAnimal[v].fullName === user.fullName) {
    //when the index value 'v' has a fullname that matches the user fullname value
    // it passes the if check and logs that object value
        return console.log(usersAnimal[v])
    //return true...
    }
  //return null
}

//etc
about 4 years ago · Santiago Trujillo 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