How do I get the following output from this array of objects?
up vote
0
down vote
favorite
This is the original array of objects:
const input = [
{firstProperty: 'something',
money: 3,
user: 'john-smith'},
{firstProperty: 'somethingDiff',
money: 7,
user: 'john-smith'},
{firstProperty: 'somElse',
money: 14,
user: 'jane-doe'},
{firstProperty: 'someOtherThing',
money: 2,
user: 'jane-doe'}]
And I would need to output the following:
const output = [
{user: 'john-smith',
totalMoney: 10},
{user: 'jane-doe',
totalMoney: 16}]
I have already achieved this result by using a bunch of for loops but I was looking for an elegant solution that uses only ES6 methods such as map, filter, reduce, etc.
javascript arrays object ecmascript-6
add a comment |
up vote
0
down vote
favorite
This is the original array of objects:
const input = [
{firstProperty: 'something',
money: 3,
user: 'john-smith'},
{firstProperty: 'somethingDiff',
money: 7,
user: 'john-smith'},
{firstProperty: 'somElse',
money: 14,
user: 'jane-doe'},
{firstProperty: 'someOtherThing',
money: 2,
user: 'jane-doe'}]
And I would need to output the following:
const output = [
{user: 'john-smith',
totalMoney: 10},
{user: 'jane-doe',
totalMoney: 16}]
I have already achieved this result by using a bunch of for loops but I was looking for an elegant solution that uses only ES6 methods such as map, filter, reduce, etc.
javascript arrays object ecmascript-6
2
Your input makes little sense.
– Jonas Wilms
Nov 10 at 12:00
This would make more sense if you change your input a bit (if possible of course). It would make more sense if the input was like this:[ {firstProperty: "something, money: 3, user:"john smith""}, {firstProperty: "somethingelse", money: 4, user:"john smith""} ]. Copy and compare and you'll see what we mean
– Mr.Turtle
Nov 10 at 12:05
Looks like you edited Data Structure from original one. Updated my answer as well.
– Nitish Narang
Nov 10 at 13:59
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This is the original array of objects:
const input = [
{firstProperty: 'something',
money: 3,
user: 'john-smith'},
{firstProperty: 'somethingDiff',
money: 7,
user: 'john-smith'},
{firstProperty: 'somElse',
money: 14,
user: 'jane-doe'},
{firstProperty: 'someOtherThing',
money: 2,
user: 'jane-doe'}]
And I would need to output the following:
const output = [
{user: 'john-smith',
totalMoney: 10},
{user: 'jane-doe',
totalMoney: 16}]
I have already achieved this result by using a bunch of for loops but I was looking for an elegant solution that uses only ES6 methods such as map, filter, reduce, etc.
javascript arrays object ecmascript-6
This is the original array of objects:
const input = [
{firstProperty: 'something',
money: 3,
user: 'john-smith'},
{firstProperty: 'somethingDiff',
money: 7,
user: 'john-smith'},
{firstProperty: 'somElse',
money: 14,
user: 'jane-doe'},
{firstProperty: 'someOtherThing',
money: 2,
user: 'jane-doe'}]
And I would need to output the following:
const output = [
{user: 'john-smith',
totalMoney: 10},
{user: 'jane-doe',
totalMoney: 16}]
I have already achieved this result by using a bunch of for loops but I was looking for an elegant solution that uses only ES6 methods such as map, filter, reduce, etc.
javascript arrays object ecmascript-6
javascript arrays object ecmascript-6
edited Nov 10 at 13:17
asked Nov 10 at 11:56
nicokruk
244
244
2
Your input makes little sense.
– Jonas Wilms
Nov 10 at 12:00
This would make more sense if you change your input a bit (if possible of course). It would make more sense if the input was like this:[ {firstProperty: "something, money: 3, user:"john smith""}, {firstProperty: "somethingelse", money: 4, user:"john smith""} ]. Copy and compare and you'll see what we mean
– Mr.Turtle
Nov 10 at 12:05
Looks like you edited Data Structure from original one. Updated my answer as well.
– Nitish Narang
Nov 10 at 13:59
add a comment |
2
Your input makes little sense.
– Jonas Wilms
Nov 10 at 12:00
This would make more sense if you change your input a bit (if possible of course). It would make more sense if the input was like this:[ {firstProperty: "something, money: 3, user:"john smith""}, {firstProperty: "somethingelse", money: 4, user:"john smith""} ]. Copy and compare and you'll see what we mean
– Mr.Turtle
Nov 10 at 12:05
Looks like you edited Data Structure from original one. Updated my answer as well.
– Nitish Narang
Nov 10 at 13:59
2
2
Your input makes little sense.
– Jonas Wilms
Nov 10 at 12:00
Your input makes little sense.
– Jonas Wilms
Nov 10 at 12:00
This would make more sense if you change your input a bit (if possible of course). It would make more sense if the input was like this:
[ {firstProperty: "something, money: 3, user:"john smith""}, {firstProperty: "somethingelse", money: 4, user:"john smith""} ]. Copy and compare and you'll see what we mean– Mr.Turtle
Nov 10 at 12:05
This would make more sense if you change your input a bit (if possible of course). It would make more sense if the input was like this:
[ {firstProperty: "something, money: 3, user:"john smith""}, {firstProperty: "somethingelse", money: 4, user:"john smith""} ]. Copy and compare and you'll see what we mean– Mr.Turtle
Nov 10 at 12:05
Looks like you edited Data Structure from original one. Updated my answer as well.
– Nitish Narang
Nov 10 at 13:59
Looks like you edited Data Structure from original one. Updated my answer as well.
– Nitish Narang
Nov 10 at 13:59
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
With the updated code it makes it even easier to get your result, my approach would still be to first aggregate and then map.
I updated the code per changes to your original post.
const input = [
{firstProperty: 'something',
money: 3,
user: 'john-smith'},
{firstProperty: 'somethingDiff',
money: 7,
user: 'john-smith'},
{firstProperty: 'somElse',
money: 14,
user: 'jane-doe'},
{firstProperty: 'someOtherThing',
money: 2,
user: 'jane-doe'}
];
function sumByUser( input ) {
// first aggregate the money per user
const moneyByUser = input.reduce( (current, { user, money }) => {
current[user] = (current[user] || 0) + money;
return current;
}, {} );
// then create an array by using the keys for user names
return Object
.keys( moneyByUser )
.map( user => ({ user, money: moneyByUser[user] }) );
}
console.log( sumByUser( input ) );
sorry, my input was wrong after all. I pasted the indexers without intention.
– nicokruk
Nov 10 at 13:06
@nicokruk well, you only need to remove the object.values statement to make it work, so it would be easy yo adapt my current code to your needs
– Icepickle
Nov 10 at 13:07
@nicokruk I updated the code, but as mentioned before this shouldn't have been a big deal as the principal still stands
– Icepickle
Nov 10 at 13:15
add a comment |
up vote
0
down vote
You can use below approach of "Array.reduce" and "Array.from" as well
var input = [{firstProperty: 'something', money: 3, user: 'john-smith'}, {firstProperty: 'somethingDiff', money: 7, user: 'john-smith'}, {firstProperty: 'somElse', money: 14, user: 'jane-doe'},{firstProperty: 'someOtherThing', money: 2, user: 'jane-doe'}]
var result = Array.from(input.reduce((m, { user, money }) =>
m.set(user, { user, totalMoney: (m.get(user) && m.get(user).totalMoney || 0) + money })
, new Map)
, ([ _, d ]) => d)
console.log(result)add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
With the updated code it makes it even easier to get your result, my approach would still be to first aggregate and then map.
I updated the code per changes to your original post.
const input = [
{firstProperty: 'something',
money: 3,
user: 'john-smith'},
{firstProperty: 'somethingDiff',
money: 7,
user: 'john-smith'},
{firstProperty: 'somElse',
money: 14,
user: 'jane-doe'},
{firstProperty: 'someOtherThing',
money: 2,
user: 'jane-doe'}
];
function sumByUser( input ) {
// first aggregate the money per user
const moneyByUser = input.reduce( (current, { user, money }) => {
current[user] = (current[user] || 0) + money;
return current;
}, {} );
// then create an array by using the keys for user names
return Object
.keys( moneyByUser )
.map( user => ({ user, money: moneyByUser[user] }) );
}
console.log( sumByUser( input ) );
sorry, my input was wrong after all. I pasted the indexers without intention.
– nicokruk
Nov 10 at 13:06
@nicokruk well, you only need to remove the object.values statement to make it work, so it would be easy yo adapt my current code to your needs
– Icepickle
Nov 10 at 13:07
@nicokruk I updated the code, but as mentioned before this shouldn't have been a big deal as the principal still stands
– Icepickle
Nov 10 at 13:15
add a comment |
up vote
2
down vote
accepted
With the updated code it makes it even easier to get your result, my approach would still be to first aggregate and then map.
I updated the code per changes to your original post.
const input = [
{firstProperty: 'something',
money: 3,
user: 'john-smith'},
{firstProperty: 'somethingDiff',
money: 7,
user: 'john-smith'},
{firstProperty: 'somElse',
money: 14,
user: 'jane-doe'},
{firstProperty: 'someOtherThing',
money: 2,
user: 'jane-doe'}
];
function sumByUser( input ) {
// first aggregate the money per user
const moneyByUser = input.reduce( (current, { user, money }) => {
current[user] = (current[user] || 0) + money;
return current;
}, {} );
// then create an array by using the keys for user names
return Object
.keys( moneyByUser )
.map( user => ({ user, money: moneyByUser[user] }) );
}
console.log( sumByUser( input ) );
sorry, my input was wrong after all. I pasted the indexers without intention.
– nicokruk
Nov 10 at 13:06
@nicokruk well, you only need to remove the object.values statement to make it work, so it would be easy yo adapt my current code to your needs
– Icepickle
Nov 10 at 13:07
@nicokruk I updated the code, but as mentioned before this shouldn't have been a big deal as the principal still stands
– Icepickle
Nov 10 at 13:15
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
With the updated code it makes it even easier to get your result, my approach would still be to first aggregate and then map.
I updated the code per changes to your original post.
const input = [
{firstProperty: 'something',
money: 3,
user: 'john-smith'},
{firstProperty: 'somethingDiff',
money: 7,
user: 'john-smith'},
{firstProperty: 'somElse',
money: 14,
user: 'jane-doe'},
{firstProperty: 'someOtherThing',
money: 2,
user: 'jane-doe'}
];
function sumByUser( input ) {
// first aggregate the money per user
const moneyByUser = input.reduce( (current, { user, money }) => {
current[user] = (current[user] || 0) + money;
return current;
}, {} );
// then create an array by using the keys for user names
return Object
.keys( moneyByUser )
.map( user => ({ user, money: moneyByUser[user] }) );
}
console.log( sumByUser( input ) );With the updated code it makes it even easier to get your result, my approach would still be to first aggregate and then map.
I updated the code per changes to your original post.
const input = [
{firstProperty: 'something',
money: 3,
user: 'john-smith'},
{firstProperty: 'somethingDiff',
money: 7,
user: 'john-smith'},
{firstProperty: 'somElse',
money: 14,
user: 'jane-doe'},
{firstProperty: 'someOtherThing',
money: 2,
user: 'jane-doe'}
];
function sumByUser( input ) {
// first aggregate the money per user
const moneyByUser = input.reduce( (current, { user, money }) => {
current[user] = (current[user] || 0) + money;
return current;
}, {} );
// then create an array by using the keys for user names
return Object
.keys( moneyByUser )
.map( user => ({ user, money: moneyByUser[user] }) );
}
console.log( sumByUser( input ) );const input = [
{firstProperty: 'something',
money: 3,
user: 'john-smith'},
{firstProperty: 'somethingDiff',
money: 7,
user: 'john-smith'},
{firstProperty: 'somElse',
money: 14,
user: 'jane-doe'},
{firstProperty: 'someOtherThing',
money: 2,
user: 'jane-doe'}
];
function sumByUser( input ) {
// first aggregate the money per user
const moneyByUser = input.reduce( (current, { user, money }) => {
current[user] = (current[user] || 0) + money;
return current;
}, {} );
// then create an array by using the keys for user names
return Object
.keys( moneyByUser )
.map( user => ({ user, money: moneyByUser[user] }) );
}
console.log( sumByUser( input ) );const input = [
{firstProperty: 'something',
money: 3,
user: 'john-smith'},
{firstProperty: 'somethingDiff',
money: 7,
user: 'john-smith'},
{firstProperty: 'somElse',
money: 14,
user: 'jane-doe'},
{firstProperty: 'someOtherThing',
money: 2,
user: 'jane-doe'}
];
function sumByUser( input ) {
// first aggregate the money per user
const moneyByUser = input.reduce( (current, { user, money }) => {
current[user] = (current[user] || 0) + money;
return current;
}, {} );
// then create an array by using the keys for user names
return Object
.keys( moneyByUser )
.map( user => ({ user, money: moneyByUser[user] }) );
}
console.log( sumByUser( input ) );edited Nov 10 at 13:14
answered Nov 10 at 12:06
Icepickle
8,24332034
8,24332034
sorry, my input was wrong after all. I pasted the indexers without intention.
– nicokruk
Nov 10 at 13:06
@nicokruk well, you only need to remove the object.values statement to make it work, so it would be easy yo adapt my current code to your needs
– Icepickle
Nov 10 at 13:07
@nicokruk I updated the code, but as mentioned before this shouldn't have been a big deal as the principal still stands
– Icepickle
Nov 10 at 13:15
add a comment |
sorry, my input was wrong after all. I pasted the indexers without intention.
– nicokruk
Nov 10 at 13:06
@nicokruk well, you only need to remove the object.values statement to make it work, so it would be easy yo adapt my current code to your needs
– Icepickle
Nov 10 at 13:07
@nicokruk I updated the code, but as mentioned before this shouldn't have been a big deal as the principal still stands
– Icepickle
Nov 10 at 13:15
sorry, my input was wrong after all. I pasted the indexers without intention.
– nicokruk
Nov 10 at 13:06
sorry, my input was wrong after all. I pasted the indexers without intention.
– nicokruk
Nov 10 at 13:06
@nicokruk well, you only need to remove the object.values statement to make it work, so it would be easy yo adapt my current code to your needs
– Icepickle
Nov 10 at 13:07
@nicokruk well, you only need to remove the object.values statement to make it work, so it would be easy yo adapt my current code to your needs
– Icepickle
Nov 10 at 13:07
@nicokruk I updated the code, but as mentioned before this shouldn't have been a big deal as the principal still stands
– Icepickle
Nov 10 at 13:15
@nicokruk I updated the code, but as mentioned before this shouldn't have been a big deal as the principal still stands
– Icepickle
Nov 10 at 13:15
add a comment |
up vote
0
down vote
You can use below approach of "Array.reduce" and "Array.from" as well
var input = [{firstProperty: 'something', money: 3, user: 'john-smith'}, {firstProperty: 'somethingDiff', money: 7, user: 'john-smith'}, {firstProperty: 'somElse', money: 14, user: 'jane-doe'},{firstProperty: 'someOtherThing', money: 2, user: 'jane-doe'}]
var result = Array.from(input.reduce((m, { user, money }) =>
m.set(user, { user, totalMoney: (m.get(user) && m.get(user).totalMoney || 0) + money })
, new Map)
, ([ _, d ]) => d)
console.log(result)add a comment |
up vote
0
down vote
You can use below approach of "Array.reduce" and "Array.from" as well
var input = [{firstProperty: 'something', money: 3, user: 'john-smith'}, {firstProperty: 'somethingDiff', money: 7, user: 'john-smith'}, {firstProperty: 'somElse', money: 14, user: 'jane-doe'},{firstProperty: 'someOtherThing', money: 2, user: 'jane-doe'}]
var result = Array.from(input.reduce((m, { user, money }) =>
m.set(user, { user, totalMoney: (m.get(user) && m.get(user).totalMoney || 0) + money })
, new Map)
, ([ _, d ]) => d)
console.log(result)add a comment |
up vote
0
down vote
up vote
0
down vote
You can use below approach of "Array.reduce" and "Array.from" as well
var input = [{firstProperty: 'something', money: 3, user: 'john-smith'}, {firstProperty: 'somethingDiff', money: 7, user: 'john-smith'}, {firstProperty: 'somElse', money: 14, user: 'jane-doe'},{firstProperty: 'someOtherThing', money: 2, user: 'jane-doe'}]
var result = Array.from(input.reduce((m, { user, money }) =>
m.set(user, { user, totalMoney: (m.get(user) && m.get(user).totalMoney || 0) + money })
, new Map)
, ([ _, d ]) => d)
console.log(result)You can use below approach of "Array.reduce" and "Array.from" as well
var input = [{firstProperty: 'something', money: 3, user: 'john-smith'}, {firstProperty: 'somethingDiff', money: 7, user: 'john-smith'}, {firstProperty: 'somElse', money: 14, user: 'jane-doe'},{firstProperty: 'someOtherThing', money: 2, user: 'jane-doe'}]
var result = Array.from(input.reduce((m, { user, money }) =>
m.set(user, { user, totalMoney: (m.get(user) && m.get(user).totalMoney || 0) + money })
, new Map)
, ([ _, d ]) => d)
console.log(result)var input = [{firstProperty: 'something', money: 3, user: 'john-smith'}, {firstProperty: 'somethingDiff', money: 7, user: 'john-smith'}, {firstProperty: 'somElse', money: 14, user: 'jane-doe'},{firstProperty: 'someOtherThing', money: 2, user: 'jane-doe'}]
var result = Array.from(input.reduce((m, { user, money }) =>
m.set(user, { user, totalMoney: (m.get(user) && m.get(user).totalMoney || 0) + money })
, new Map)
, ([ _, d ]) => d)
console.log(result)var input = [{firstProperty: 'something', money: 3, user: 'john-smith'}, {firstProperty: 'somethingDiff', money: 7, user: 'john-smith'}, {firstProperty: 'somElse', money: 14, user: 'jane-doe'},{firstProperty: 'someOtherThing', money: 2, user: 'jane-doe'}]
var result = Array.from(input.reduce((m, { user, money }) =>
m.set(user, { user, totalMoney: (m.get(user) && m.get(user).totalMoney || 0) + money })
, new Map)
, ([ _, d ]) => d)
console.log(result)edited Nov 10 at 13:58
answered Nov 10 at 12:38
Nitish Narang
1,27269
1,27269
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238693%2fhow-do-i-get-the-following-output-from-this-array-of-objects%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
Your input makes little sense.
– Jonas Wilms
Nov 10 at 12:00
This would make more sense if you change your input a bit (if possible of course). It would make more sense if the input was like this:
[ {firstProperty: "something, money: 3, user:"john smith""}, {firstProperty: "somethingelse", money: 4, user:"john smith""} ]. Copy and compare and you'll see what we mean– Mr.Turtle
Nov 10 at 12:05
Looks like you edited Data Structure from original one. Updated my answer as well.
– Nitish Narang
Nov 10 at 13:59