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

230
Visualizações
How can i check if the child object has the id inside it?

I have seller and buyer information inside an array, i need to check if the id matches with either seller or buyer id inside the array, then i need to remove the object from the array.

here is the array:

[
  {
    "transactionId": null,
    "buyer": {
      "userId": "eu-central-1",
      "email": "pic@gmail.com",
      "firstName": "pic",
      "lastName": "1",
      "_id": "5902ca3ce201550655a7bbf8"
    },
    "seller": {
      "userId": "eu-central-2",
      "email": "pic2@gmail.com",
      "firstName": "pic",
      "lastName": "2",
      "_id": "5902ca3ce201550655a7bbf7"
    }
  }
]

i have the userid eu-central-2. i want the expected output as,

[
  {
    "transactionId": null,
    "user": {
      "userId": "eu-central-1",
      "email": "pic@gmail.com",
      "firstName": "pic",
      "lastName": "1",
      "_id": "5902ca3ce201550655a7bbf8"
    } 
  }
]

what i tried

  let data = results.filter(result=> seller.userId || buyer.userId == userId);

the problem is the removal part and renaming the User.

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

0

var userId = 'eu-central-2';
var results = [
{
    "transactionId": null,
    "buyer": {
      "userId": "eu-central-1",
      "email": "pic@gmail.com",
      "firstName": "pic",
      "lastName": "1",
      "_id": "5902ca3ce201550655a7bbf8"
    },
    "seller": {
      "userId": "eu-central-2",
      "email": "pic2@gmail.com",
      "firstName": "pic",
      "lastName": "2",
      "_id": "5902ca3ce201550655a7bbf7"
    }
  }
];

If you want to remove the entire object, use this one:

var data = results.filter(function(a) {
   if(a.buyer.userId == userId || a.seller.userId == userId) {
     return false;
   }
    return true;
});

Or if you want to remove the specific buyer or seller, use this:

var data = results.filter(function(a) {
   if(a.buyer.userId == userId) {
     delete a.buyer;
   } else if(a.seller.userId == userId) {
     delete a.seller;
   }

   if(a.buyer) {
     a['user'] = a.buyer;
     delete a.buyer;
   } else if(a.seller) {
     a['user'] = a.seller;
     delete a.seller;
   }
    return true;
});

console.log(data);
over 4 years ago · Santiago Trujillo Relatório

0

You need to be a bit more specific with your statement because javascript might not do what you expect. If I understand it correctly you want the following:

seller.userId === userId
OR
buyer.userId === userId

If that is what you want the used statement wouldnt work because JS will see it as:

seller.userId === true
OR
buyer.userId === userId

What you probebly need to do is just mention userId twice and use parenthesis like this: (seller.userId == userId) || (buyer.userId == userId)

over 4 years ago · Santiago Trujillo Relatório

0

I would suggest the following code:

results.map(result => ({ 
        transactionId: result.transactionId, 
        user: [result.buyer, result.seller]
            [1-[result.buyer, result.seller].findIndex(user => user.userId === userId)]
    })).filter(result => result.user);

const results = [
  {
    "transactionId": null,
    "buyer": {
      "userId": "eu-central-1",
      "email": "pic@gmail.com",
      "firstName": "pic",
      "lastName": "1",
      "_id": "5902ca3ce201550655a7bbf8"
    },
    "seller": {
      "userId": "eu-central-2",
      "email": "pic2@gmail.com",
      "firstName": "pic",
      "lastName": "2",
      "_id": "5902ca3ce201550655a7bbf7"
    }
  },  {
    "transactionId": null,
    "buyer": {
      "userId": "eu-central-3",
      "email": "pic@gmail.com",
      "firstName": "pic",
      "lastName": "1",
      "_id": "5902ca3ce201550655a7bbf8"
    },
    "seller": {
      "userId": "eu-central-4",
      "email": "pic2@gmail.com",
      "firstName": "pic",
      "lastName": "2",
      "_id": "5902ca3ce201550655a7bbf7"
    }
  }
];

const userId = "eu-central-2";
const data = results.map(result => ({ 
        transactionId: result.transactionId, 
        user: [result.buyer, result.seller]
            [1-[result.buyer, result.seller].findIndex(user => user.userId === userId)]
    })).filter(result => result.user);

console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

over 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