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.
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);
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)
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; }