I am trying to create an Object with the name and user id for each service, I want the object to look like this:
const object = {
name: Netflix, user: (user id who pays max price for this provider)
name: Comcast, user: (same)
name: Verizon, user: (same)
}
I have tried changing the object property in the map return but it's not working, also I already have the object half done I mean with the names of the providers now I need the other key,value pair
const services = [
{ userid: 1, providerId: 1, amount: 250000 },
{ userid: 4, providerId: 3, amount: 280900 },
{ userid: 6, providerId: 3, amount: 31000 },
{ userid: 2, providerId: 2, amount: 58600 },
{ userid: 3, providerId: 1, amount: 13000 },
{ userid: 5, providerId: 2, amount: 5000 },
{ userid: 3, providerId: 3, amount: 59900 },
{ userid: 6, providerId: 3, amount: 9500 }
]
const providers = [
{ id: 1, name: Netflix },
{ id: 2, name: Comcast },
{ id: 3, name: Verizon }
]
This is my function
function getUserId(providerId) {
return services.filter(function(obj) {
if (obj.providerId == providerId)
return obj.providerId;
}).map(function(obj) { return obj.amount });
}
function getMaxUserId(providerId) {
return Math.max(...getUserId(providerId));
}
providers.forEach(prov => {
object[prov.name] = getUserId(prov.id);
})
as you can see first I filter the entire array looking for the providers with certain providerId then I create a new array with map function filled with all the 'amounts' for that particular provider and last I search for the max amount of that array, this all works fine it return the right max amount for each provider BUT I also want to get the user id who's paying the most for each provider
actually I'm getting my object like this:
[
{name: Netflix, user: 250000},
{name: Comcast, user: 58600},
{name: Verizon, user: 280900}
]
and I need this:
[
{name: Netflix, user: 1},
{name: Comcast, user: 2},
{name: Verizon, user: 4}
]