Nod*_*rjs 7 javascript arrays object
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)
}
Run Code Online (Sandbox Code Playgroud)
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 }
]
Run Code Online (Sandbox Code Playgroud)
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);
})
Run Code Online (Sandbox Code Playgroud)
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}
]
Run Code Online (Sandbox Code Playgroud)
and I need this:
[
{name: Netflix, user: 1},
{name: Comcast, user: 2},
{name: Verizon, user: 4}
]
Run Code Online (Sandbox Code Playgroud)
而且,作为问题的第三种可能的解决方案(跟进我的评论),您可以使用类似的东西(您可以修改对象,但是您认为更符合您的喜好,但我建议完全摆脱name
和user
键):
const object = {};
function getUserId(providerId) {
return services.filter(function(obj) {
if (obj.providerId == providerId)
return obj.providerId;
}).map(function(obj) { return obj });
}
function getMaxUserId(output) {
let amm = output.map( (o) => { return o.amount; } );
let idx = amm.indexOf(Math.max(...amm));
return output[idx].userid;
}
providers.forEach(prov => {
object[prov.name] = getMaxUserId(getUserId(prov.id));
})
console.log(object)
Run Code Online (Sandbox Code Playgroud)
使用此解决方案的情况下的输出将如下所示:
Object { Netflix: 1, Comcast: 2, Verizon: 4 }
Run Code Online (Sandbox Code Playgroud)
Ofc,通过一个小的调整,您将能够Array
按照最初的预期返回一个 with 对象(不要忘记分配object
给[]
):
providers.forEach(prov => {
object.push( {name:prov.name,user:getMaxUserId(getUserId(prov.id))} )
})
Run Code Online (Sandbox Code Playgroud)
在后一种情况下,输出将如下所示:
Array [Object { name: "Netflix", user: 1 }, Object { name: "Comcast", user: 2 }, Object { name: "Verizon", user: 4 }]
Run Code Online (Sandbox Code Playgroud)