I want to loop over the data object and pick two items i.e. year and revenue from it and put it into a new object
var data [{"year": 2014, "revenue": 20000, "costs": 10000, "hours": 50}, {"year": 2015, "revenue": 30000, "costs": 20000, "hours": 54}, {"year": 2016, "revenue": 30000, "costs": 10000, "hours": 40}]
var pickedData = {};
data.forEach(output)
function output(d) {
Object.assign({}, pickedData.Year = d.year),
Object.assign({}, pickedData.Revenue = d.revenue)
}
The code works but assign only the last item in data to the new object pickedData which means it is looping it over but not assigning value to the object everytime it goes through. How can I have output like this:
console.log(pickedData)
[{Year: 2014, Revenue: 20000}, {Year: 2015, Revenue: 30000}, {Year: 2016, Revenue: 30000}]