简体   繁体   English

JavaScript 如何将对象数组中的值获取到数组数组中,以便每个子数组对来自类似键的值进行分组?

[英]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.reduceObject.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.

相关问题 如何在javascript中过滤对象子数组的数组和对象子数组的数组 - how to filtering array of sub-array of objects and array of sub-array of objects in javascript 如何从 JavaScript 中的 3 个对象数组中排序并获取每个名称的最大分数,并将其以及缺少的键和值分配给另一个数组? - How to sort and get max score for each name from 3 array of objects in JavaScript and assign it and the missing keys and values to another array? 如何从对象数组中的嵌套数组中获取数组值的组合 - How to get the combination of array values from nested arrays in an array of objects 如何使用另一个对象数组的键和 arrays 数组中的值来构造对象数组? - How to construct an array of objects by using the keys of another array of objects and values from the array of arrays? 如何从javascript中的对象数组中获取值 - How to get values from array of objects in javascript 如何从高性能的两个阵列中提取子阵列? - How to extract a sub-array from two arrays with high performances? JavaScript:如何打印数组数组中的所有键和值? - JavaScript: How to print all Keys and Values from Array of Arrays? 如何从对象数组中获取具有特定键的唯一值的对象? - How to get objects with unique values for specific keys from array of objects? 从每个对象数组中获取最大值 - Get max values from each array of objects 从每个子数组返回数量最大的数组 - Returning an array with the largest number from each sub-array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM