Reputation: 5228
How to get acces to specific object based on property from API response? In my scenerio i have array of Products (i get it properly from my server), watchedProducts
class have property productId
that is the same as id
in Product
object. My question is, how iterate properly on that arrays (with the best performance) to get products
, that watchedProduct.productId == product.id
?
Code:
export class MyProductsComponent implements OnInit {
watchedProducts: WatchedProduct[] = [];
products: Product[] = [];
constructor(private dataService: DataService) {}
ngOnInit() {
this.getWatchedProducts();
}
getWatchedProducts(): void {
this.dataService.getObjects(WatchedProduct).subscribe(result => {
this.watchedProducts = result.object;
//get watched product, its work fine
});
this.dataService.getObjects(Product).subscribe(result => {
this.products = ...
//what to do next?
//i want to get that products, where watchedProducts[i].productId == products[i].id
});
}
}
EDIT
I have my SQL Server where table Products
is related to WatchedProducts
like parent-child. ProductId
in WatchedProducts is FK for Id
in Products
products
looks like this:
And watchedProducts
:
In this scenerio, Product.id == 8 === WatchedProduct.ProductId == 8
My question is, how to get products
array where watchedProduct.productId == product.Id
?
EDIT2
Based on one answer i make that thing:
getWatchedProducts(): void {
this.dataService.getObjects(WatchedProduct).subscribe(result => {
this.errorService.showError(result);
this.watched = result.object;
});
this.dataService.getObjects(Product).subscribe(result => {
this.errorService.showError(result);
this.products = result.object;
for (let i = 0; i < this.watched.length; i++) {
for (let j = 0; j < this.products.length; j++) {
if (this.watched[i].productId == this.products[j].id)
this.watchedProducts.push(this.products[j]);
}
}
});
}
Sometimes i need refresh browser couple of times to get get respons with products objects (to see what i want to get, sometims it works ok, sometimes i get nothing from that func). Could you tell me, what im doing wrong here?:
Upvotes: 0
Views: 826
Reputation: 57929
No, @michasaucer. When you make two subscribe to two different observables, you can not know wich observable finished first. So you can "join" the two calls in only one.
e.g. you can use forkJoin
getWatchedProducts(): void {
forkJoin(this.dataService.getObjects(WatchedProduct),
this.dataService.getObjects(Product))
.subscribe(result=>{
// in result[0] you has the response to getObject(WatchedProduct)
// in result[1] you has the response to getObjects(Product)
this.errorService.showError(result[0]);
this.watched = result[0].object;
this.errorService.showError(result[1]);
this.products = result[1].object;
for (let i = 0; i < this.watched.length; i++) {
for (let j = 0; j < this.products.length; j++) {
if (this.watched[i].productId == this.products[j].id)
this.watchedProducts.push(this.products[j]);
}
}
})
}
See that there are a unique subscribe NOTE: I not revised your code, just use forkjoin
Upvotes: 1
Reputation: 6759
If you want to get only one Product
from array, not all of them. Product
, that have same id
value as Watchedproduct
then you would need to add Conditional
rule such as if
.
this.dataService.getObjects(Product).subscribe(result => {
//what to do next?
//i want to get that products, where watchedProducts[i].productId == products[i].id
for (let i=0; i<this.watchedProducts.length; i++) {
if (result[i].productId==this.watchedProducts.id) {
//do your thing to store it.
}
}
});
Upvotes: 0