Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Post Timeline

A basic plan is to make a new object keyed to the month and add each year as you go through. recducereduce() makes it pretty concise:

let dates = [{"year": 2017, "month": "Jan", "revenue": 2000}, {"year": 2017, "month": "Feb", "revenue": 3000}, {"year": 2017, "month": "Mar", "revenue": 1000}, {"year": 2016, "month": "Jan", "revenue": 5000}, {"year": 2016, "month": "Feb", "revenue": 4000}, {"year": 2016, "month": "Mar", "revenue": 2000}]
// You only want an array of values, so just return 
// Object.values()
let d = Object.values(dates.reduce((a, c) =>{
 // either use the existing entry or create a new one
 // with month already set
 (a[c.month] || (a[c.month] = {Month: c.month}))[c.year] = c.revenuerevenue;
 return aa;
}, {}))
console.log(d)

:

A basic plan is to make a new object keyed to the month and add each year as you go through. recduce() makes it pretty concise:

let dates = [{"year": 2017, "month": "Jan", "revenue": 2000}, {"year": 2017, "month": "Feb", "revenue": 3000}, {"year": 2017, "month": "Mar", "revenue": 1000}, {"year": 2016, "month": "Jan", "revenue": 5000}, {"year": 2016, "month": "Feb", "revenue": 4000}, {"year": 2016, "month": "Mar", "revenue": 2000}]
// You only want an array of values, so just return 
// Object.values()
let d = Object.values(dates.reduce((a, c) =>{
 // either use the existing entry or create a new one
 // with month already set
 (a[c.month] || (a[c.month] = {Month: c.month}))[c.year] = c.revenue
 return a
}, {}))
console.log(d)

:

A basic plan is to make a new object keyed to the month and add each year as you go through. reduce() makes it pretty concise:

let dates = [{"year": 2017, "month": "Jan", "revenue": 2000}, {"year": 2017, "month": "Feb", "revenue": 3000}, {"year": 2017, "month": "Mar", "revenue": 1000}, {"year": 2016, "month": "Jan", "revenue": 5000}, {"year": 2016, "month": "Feb", "revenue": 4000}, {"year": 2016, "month": "Mar", "revenue": 2000}]
// You only want an array of values, so just return Object.values()
let d = Object.values(dates.reduce((a, c) =>{
 // either use the existing entry or create a new one
 // with month already set
 (a[c.month] || (a[c.month] = {Month: c.month}))[c.year] = c.revenue;
 return a;
}, {}))
console.log(d)

:

edited body
Source Link
Mark
  • 92.7k
  • 8
  • 116
  • 156

A basic plan is to make a new object keyed to the month and add each year as you go through. recduce() makes it pretty concise:

let dates = [{"year": 2017, "month": "Jan", "revenue": 2000}, {"year": 2017, "month": "Feb", "revenue": 3000}, {"year": 2017, "month": "Mar", "revenue": 1000}, {"year": 2016, "month": "Jan", "revenue": 5000}, {"year": 2016, "month": "Feb", "revenue": 4000}, {"year": 2016, "month": "Mar", "revenue": 2000}]
// You only want an array of values, so just return 
// Object.values()
let d = Object.values(dates.reduce((a, c) =>{
 // either use the existing entry or create a new one
 // with month already set
 (a[c.month] || (a[c.month] = {monthMonth: c.month}))[c.year] = c.revenue
 return a
}, {}))
console.log(d)

:

A basic plan is to make a new object keyed to the month and add each year as you go through. recduce() makes it pretty concise:

let dates = [{"year": 2017, "month": "Jan", "revenue": 2000}, {"year": 2017, "month": "Feb", "revenue": 3000}, {"year": 2017, "month": "Mar", "revenue": 1000}, {"year": 2016, "month": "Jan", "revenue": 5000}, {"year": 2016, "month": "Feb", "revenue": 4000}, {"year": 2016, "month": "Mar", "revenue": 2000}]
// You only want an array of values, so just return 
// Object.values()
let d = Object.values(dates.reduce((a, c) =>{
 // either use the existing entry or create a new one
 // with month already set
 (a[c.month] || (a[c.month] = {month: c.month}))[c.year] = c.revenue
 return a
}, {}))
console.log(d)

:

A basic plan is to make a new object keyed to the month and add each year as you go through. recduce() makes it pretty concise:

let dates = [{"year": 2017, "month": "Jan", "revenue": 2000}, {"year": 2017, "month": "Feb", "revenue": 3000}, {"year": 2017, "month": "Mar", "revenue": 1000}, {"year": 2016, "month": "Jan", "revenue": 5000}, {"year": 2016, "month": "Feb", "revenue": 4000}, {"year": 2016, "month": "Mar", "revenue": 2000}]
// You only want an array of values, so just return 
// Object.values()
let d = Object.values(dates.reduce((a, c) =>{
 // either use the existing entry or create a new one
 // with month already set
 (a[c.month] || (a[c.month] = {Month: c.month}))[c.year] = c.revenue
 return a
}, {}))
console.log(d)

:

Source Link
Mark
  • 92.7k
  • 8
  • 116
  • 156

A basic plan is to make a new object keyed to the month and add each year as you go through. recduce() makes it pretty concise:

let dates = [{"year": 2017, "month": "Jan", "revenue": 2000}, {"year": 2017, "month": "Feb", "revenue": 3000}, {"year": 2017, "month": "Mar", "revenue": 1000}, {"year": 2016, "month": "Jan", "revenue": 5000}, {"year": 2016, "month": "Feb", "revenue": 4000}, {"year": 2016, "month": "Mar", "revenue": 2000}]
// You only want an array of values, so just return 
// Object.values()
let d = Object.values(dates.reduce((a, c) =>{
 // either use the existing entry or create a new one
 // with month already set
 (a[c.month] || (a[c.month] = {month: c.month}))[c.year] = c.revenue
 return a
}, {}))
console.log(d)

:

lang-js

AltStyle によって変換されたページ (->オリジナル) /