[英]JavaScript How to get values from an array of objects into array of arrays so that each sub-array groups values from like keys?
I have an array of objects based on user input, for example:我有一个基于用户输入的对象数组,例如:
const sortedByDate = [
{ Date: '2020-05-03', Genre: 'History', PageViews: '34' },
{ Date: '2010-01-16', Genre: 'History', PageViews: '10' },
{ Date: '1975-04-23', Genre: 'Literature', PageViews: '27' },
{ Date: '1997-10-14', Genre: 'History', PageViews: '3' },
{ Date: '1990-08-22', Genre: 'Literature', PageViews: '42' },
{ Date: '2001-06-13', Genre: 'Literature', PageViews: '64' }
]
How can I get the PageView values into an array of arrays such that each sub-array represents one Genre?如何将 PageView 值放入数组数组中,以便每个子数组代表一个流派? For example:
例如:
const chartData = [
[ 34, 10, 3 ], // These are the PageView values from the Genre 'History'
[ 27, 42, 64 ] // These are the PageView values from the Genre 'Literature'
]
The sortedByDate array above will have an arbitrary number of elements based on user input, and the chartData array needs to have an arbitrary number of sub-arrays based on the number of "Genres"上面的 sortedByDate 数组将具有基于用户输入的任意数量的元素,而 chartData 数组需要具有基于“流派”数量的任意数量的子数组
The expected output can be achieved by using Array.reduce
and Object.values
可以通过使用
Array.reduce
和Object.values
来实现预期的输出
const sortedByDate = [ { Date: '2020-05-03', Genre: 'History', PageViews: '34' }, { Date: '2010-01-16', Genre: 'History', PageViews: '10' }, { Date: '1975-04-23', Genre: 'Literature', PageViews: '27' }, { Date: '1997-10-14', Genre: 'History', PageViews: '3' }, { Date: '1990-08-22', Genre: 'Literature', PageViews: '42' }, { Date: '2001-06-13', Genre: 'Literature', PageViews: '64' } ] const getFormattedData = (data) => { const finalRes = data.reduce((res, { Genre, PageViews }) => { if (res[Genre]) { res[Genre].push(PageViews) } else { res[Genre] = [PageViews] } return res; }, {}); return Object.values(finalRes); } console.log(getFormattedData(sortedByDate))
.as-console-wrapper { max-height: 100% !important; }
You can reduce the array of objects to an object of arrays using the Genre
property as the key.您可以使用
Genre
属性作为键将对象数组缩减为数组对象。 Convert the object to an array using Object.values()
:使用
Object.values()
将对象转换为数组:
const sortedByDate = [{"Date":"2020-05-03","Genre":"History","PageViews":"34"},{"Date":"2010-01-16","Genre":"History","PageViews":"10"},{"Date":"1975-04-23","Genre":"Literature","PageViews":"27"},{"Date":"1997-10-14","Genre":"History","PageViews":"3"},{"Date":"1990-08-22","Genre":"Literature","PageViews":"42"},{"Date":"2001-06-13","Genre":"Literature","PageViews":"64"}] const fn = (data) => Object.values( // convert to an array of arrays data.reduce((r, { Genre, PageViews }) => { r[Genre] = r[Genre] || [] // create key by Genre if doesn't exist r[Genre].push(PageViews) // push to Genre key array return r; }, {}) ); const result = fn(sortedByDate) console.log(result)
.as-console-wrapper { max-height: 100% !important; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.