JQuery - show only one if the same id and also add it on weeks [duplicate]
up vote
2
down vote
favorite
This question already has an answer here:
Most efficient method to groupby on a array of objects
33 answers
I have this sample data:
[{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}]
I wanted that I will only show one if id is redundant but add all of the weeks that the id's had
My Attempt:
var data = [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}];
var weeks = 0;
for (var i=0; i<data.length; i++){
weeks += data[i].weeks;
}
Desired output
[{"id":"2", "weeks":6}, {"id":3, "weeks":7}]
javascript jquery
marked as duplicate by Andreas, Amit Joki
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 at 6:27
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
2
down vote
favorite
This question already has an answer here:
Most efficient method to groupby on a array of objects
33 answers
I have this sample data:
[{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}]
I wanted that I will only show one if id is redundant but add all of the weeks that the id's had
My Attempt:
var data = [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}];
var weeks = 0;
for (var i=0; i<data.length; i++){
weeks += data[i].weeks;
}
Desired output
[{"id":"2", "weeks":6}, {"id":3, "weeks":7}]
javascript jquery
marked as duplicate by Andreas, Amit Joki
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 at 6:27
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
This question already has an answer here:
Most efficient method to groupby on a array of objects
33 answers
I have this sample data:
[{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}]
I wanted that I will only show one if id is redundant but add all of the weeks that the id's had
My Attempt:
var data = [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}];
var weeks = 0;
for (var i=0; i<data.length; i++){
weeks += data[i].weeks;
}
Desired output
[{"id":"2", "weeks":6}, {"id":3, "weeks":7}]
javascript jquery
This question already has an answer here:
Most efficient method to groupby on a array of objects
33 answers
I have this sample data:
[{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}]
I wanted that I will only show one if id is redundant but add all of the weeks that the id's had
My Attempt:
var data = [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}];
var weeks = 0;
for (var i=0; i<data.length; i++){
weeks += data[i].weeks;
}
Desired output
[{"id":"2", "weeks":6}, {"id":3, "weeks":7}]
This question already has an answer here:
Most efficient method to groupby on a array of objects
33 answers
javascript jquery
javascript jquery
asked Nov 20 at 6:24
sad saddest
338
338
marked as duplicate by Andreas, Amit Joki
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 at 6:27
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Andreas, Amit Joki
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 at 6:27
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
You can use array#reduce()
with Object.values()
. Group your data based on id
and add the weeks
for repeating id
in an object. Take the values from the object using Object.values()
.
const data = [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}],
result = Object.values(data.reduce((r,{id,weeks}) => {
r[id] = r[id] || {id, weeks : 0};
r[id].weeks += weeks;
return r;
},{}));
console.log(result);
add a comment |
up vote
1
down vote
You can Array.prototype.reduce() combined with Array.prototype.push() or Array.prototype.find() based on the number of repeated elements with same id
Code:
const data = [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}];
const result = data
.reduce((a, c) => {
a.temp[c.id] = ++a.temp[c.id] || 1;
a.temp[c.id] === 1
? a.array.push(c)
: a.array.find(item => item.id == a.temp[c.id]).weeks += c.weeks;
return a;
}, {temp: {}, array: })
.array;
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
3
down vote
accepted
You can use array#reduce()
with Object.values()
. Group your data based on id
and add the weeks
for repeating id
in an object. Take the values from the object using Object.values()
.
const data = [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}],
result = Object.values(data.reduce((r,{id,weeks}) => {
r[id] = r[id] || {id, weeks : 0};
r[id].weeks += weeks;
return r;
},{}));
console.log(result);
add a comment |
up vote
3
down vote
accepted
You can use array#reduce()
with Object.values()
. Group your data based on id
and add the weeks
for repeating id
in an object. Take the values from the object using Object.values()
.
const data = [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}],
result = Object.values(data.reduce((r,{id,weeks}) => {
r[id] = r[id] || {id, weeks : 0};
r[id].weeks += weeks;
return r;
},{}));
console.log(result);
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
You can use array#reduce()
with Object.values()
. Group your data based on id
and add the weeks
for repeating id
in an object. Take the values from the object using Object.values()
.
const data = [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}],
result = Object.values(data.reduce((r,{id,weeks}) => {
r[id] = r[id] || {id, weeks : 0};
r[id].weeks += weeks;
return r;
},{}));
console.log(result);
You can use array#reduce()
with Object.values()
. Group your data based on id
and add the weeks
for repeating id
in an object. Take the values from the object using Object.values()
.
const data = [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}],
result = Object.values(data.reduce((r,{id,weeks}) => {
r[id] = r[id] || {id, weeks : 0};
r[id].weeks += weeks;
return r;
},{}));
console.log(result);
const data = [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}],
result = Object.values(data.reduce((r,{id,weeks}) => {
r[id] = r[id] || {id, weeks : 0};
r[id].weeks += weeks;
return r;
},{}));
console.log(result);
const data = [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}],
result = Object.values(data.reduce((r,{id,weeks}) => {
r[id] = r[id] || {id, weeks : 0};
r[id].weeks += weeks;
return r;
},{}));
console.log(result);
answered Nov 20 at 6:29
Hassan Imam
11.8k31230
11.8k31230
add a comment |
add a comment |
up vote
1
down vote
You can Array.prototype.reduce() combined with Array.prototype.push() or Array.prototype.find() based on the number of repeated elements with same id
Code:
const data = [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}];
const result = data
.reduce((a, c) => {
a.temp[c.id] = ++a.temp[c.id] || 1;
a.temp[c.id] === 1
? a.array.push(c)
: a.array.find(item => item.id == a.temp[c.id]).weeks += c.weeks;
return a;
}, {temp: {}, array: })
.array;
console.log(result);
add a comment |
up vote
1
down vote
You can Array.prototype.reduce() combined with Array.prototype.push() or Array.prototype.find() based on the number of repeated elements with same id
Code:
const data = [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}];
const result = data
.reduce((a, c) => {
a.temp[c.id] = ++a.temp[c.id] || 1;
a.temp[c.id] === 1
? a.array.push(c)
: a.array.find(item => item.id == a.temp[c.id]).weeks += c.weeks;
return a;
}, {temp: {}, array: })
.array;
console.log(result);
add a comment |
up vote
1
down vote
up vote
1
down vote
You can Array.prototype.reduce() combined with Array.prototype.push() or Array.prototype.find() based on the number of repeated elements with same id
Code:
const data = [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}];
const result = data
.reduce((a, c) => {
a.temp[c.id] = ++a.temp[c.id] || 1;
a.temp[c.id] === 1
? a.array.push(c)
: a.array.find(item => item.id == a.temp[c.id]).weeks += c.weeks;
return a;
}, {temp: {}, array: })
.array;
console.log(result);
You can Array.prototype.reduce() combined with Array.prototype.push() or Array.prototype.find() based on the number of repeated elements with same id
Code:
const data = [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}];
const result = data
.reduce((a, c) => {
a.temp[c.id] = ++a.temp[c.id] || 1;
a.temp[c.id] === 1
? a.array.push(c)
: a.array.find(item => item.id == a.temp[c.id]).weeks += c.weeks;
return a;
}, {temp: {}, array: })
.array;
console.log(result);
const data = [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}];
const result = data
.reduce((a, c) => {
a.temp[c.id] = ++a.temp[c.id] || 1;
a.temp[c.id] === 1
? a.array.push(c)
: a.array.find(item => item.id == a.temp[c.id]).weeks += c.weeks;
return a;
}, {temp: {}, array: })
.array;
console.log(result);
const data = [{"id":"2", "weeks":2}, {"id":"2", "weeks":4}, {"id":3, "weeks":7}];
const result = data
.reduce((a, c) => {
a.temp[c.id] = ++a.temp[c.id] || 1;
a.temp[c.id] === 1
? a.array.push(c)
: a.array.find(item => item.id == a.temp[c.id]).weeks += c.weeks;
return a;
}, {temp: {}, array: })
.array;
console.log(result);
edited Nov 20 at 7:11
answered Nov 20 at 6:29
Yosvel Quintero
10.8k42229
10.8k42229
add a comment |
add a comment |