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

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

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 Denunciar

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 Denunciar

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