Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

227
Views
¿Cómo puedo verificar si el objeto secundario tiene la identificación dentro?

Tengo información del vendedor y del comprador dentro de una matriz, necesito verificar si la identificación coincide con la identificación del vendedor o del comprador dentro de la matriz, luego necesito eliminar el objeto de la matriz.

aquí está la matriz:

 [ { "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" } } ]

Tengo el ID de usuario eu-central-2 . quiero el expected output como,

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

lo que intenté

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

el problema es la parte de eliminación y el cambio de nombre del usuario.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

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" } } ];

Si desea eliminar todo el objeto, use este:

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

O si desea eliminar el comprador o vendedor específico, use esto:

 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 Report

0

Debe ser un poco más específico con su declaración porque es posible que javascript no haga lo que espera. Si lo entiendo correctamente, desea lo siguiente:

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

Si eso es lo que desea, la instrucción used no funcionaría porque JS lo verá como:

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

Lo que probablemente necesita hacer es mencionar el ID de usuario dos veces y usar paréntesis como este: (seller.userId == userId) || (buyer.userId == userId)

over 4 years ago · Santiago Trujillo Report

0

Yo sugeriría el siguiente código:

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!