I am working with two arrays in TypeScript. The first one is a products
array containing objects with product names and IDs, like this:
const products =
[
{ product: 'prod_aaa', name: 'Starter' },
{ product: 'prod_bbb', name: 'Growth' },
{ product: 'prod_ccc', name: 'Elite' },
];
The second array is a subscriptions
array with details of active subscriptions, shown below:
const subscriptions =
[
{
"status":"active",
"product": "prod_aaa",
"quantity": 1,
},
{
"status":"active",
"product": "prod_2342352345",
"quantity": 1,
}
];
My goal now is to search the subscriptions
array to find the first matching product from the products
array, and then display the name of that product (such as Starter/Growth/Elite).
I assume I may need to use subscriptions.filter()
, but I am unsure how to extract the name of the matched product.
Although I cannot modify the subscriptions
array, I can make changes to the products
array if required.
What would be the most effective approach to achieve this task?
By the way, my script is coded in TypeScript.