创建来自数组对象数组的新数组,具有特定值
我有以下数据,我想使用JavaScript制作一个新数组。 我试图两次进行循环,但无法获得理想的结果。
data: (5) [{…}, {…}, {…}, {…}, {…}]
// above data contains below:
[{
id: 1,
productName: "Tacos",
productData:
[ { status: 1, ready: 85, total: 262},
{ status: 2, ready: 59, total: 16},
{ status: 3, ready: 67, total: 168},
{ status: 4, ready: 42, total: 301},
{ status: 5, ready: 10, total: 266},
{ status: 6, ready: 56, total: 220},
{ status: 7, ready: 28, total: 173}] },
{
id: 2
,
productName: "Poke",
productData:
[ { status: 1, ready: 85, total: 300},
{ status: 2, ready: 59, total: 150},
{ status: 3, ready: 67, total: 93},
{ status: 4, ready: 42, total: 173},
{ status: 5, ready: 10, total: 266},
{ status: 6, ready: 56, total: 98},
{ status: 7, ready: 28, total: 121}] },
{
id: 3
productName: "Sandwich",
productData:
[ { status: 1, ready: 85, total: 141},
{ status: 2, ready: 59, total: 230},
{ status: 3, ready: 67, total: 155},
{ status: 4, ready: 42, total: 167},
{ status: 5, ready: 10, total: 98},
{ status: 6, ready: 56, total: 145},
{ status: 7, ready: 28, total: 123}]
},
{
id: 4
productName: “Burrito”,
productData:
[ { status: 1, ready: 85, total: 45},
{ status: 2, ready: 59, total: 62},
{ status: 3, ready: 67, total: 77},
{ status: 4, ready: 42, total: 21},
{ status: 5, ready: 10, total: 33},
{ status: 6, ready: 56, total: 85},
{ status: 7, ready: 28, total: 35}]
},
{
id: 5
productName: “Gyoza”,
productData:
[ { status: 1, ready: 85, total: 88},
{ status: 2, ready: 59, total: 83},
{ status: 3, ready: 67, total: 103},
{ status: 4, ready: 42, total: 98},
{ status: 5, ready: 10, total: 99},
{ status: 6, ready: 56, total: 120},
{ status: 7, ready: 28, total: 96}]
},
]
结果应该是这样。 来自每个数组中的数字数组数组中具有相同状态数据的数字。
[[262, 300, 141, 45, 88], // All numbers, value of total from status: 1 from each array of productData
[16, 150, 230, 62, 83], // status: 2
[168, 301, 93, 155, 77], // status: 3
...]
先感谢您。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用 /code> 与降低() 。
首先,我们将数组弄平,因此我们获得一个包含所有
productData
的巨大数组:然后在此之后
redair()
将使用status status 作为收集具有相同状态的所有值的钥匙。
我们从一个空对象开始,并检查每个元素是否已经遇到了该状态。如果没有,我们创建一个包含此元素的数组
total
值。如果我们只有push()
该数组的另一个值。最后但并非最不重要的一点是,我们只需要获取JavaScript对象的值,因为我们对密钥不感兴趣。可以使用
object。 values()
。You should use
flatMap()
in conjunction withreduce()
.First we flatten the array, so we get one huge array containing all
productData
:And after that
reduce()
will use a JavaScript object with thestatus
as a key to collect all the values with the same status.We start off with an empty object and check for each element whether we have encountered that status already. If we have not, we create an array containing this element
total
value. In case we have we justpush()
another value to that array.Last but not least, we only need to get the values of the JavaScript object as we are not interested in the keys. This can be done using
Object.values()
.这可以通过将单个值映射为子阵列,然后将结果“缩放”来实现。
请参阅:属性的提取值和 javascript'> javascript a>以进一步讨论这两个操作。
This can be achieved by mapping the individual values as sub-arrays and then 'zipping' the result.
see: From an array of objects, extract value of a property as array and Javascript equivalent of Python's zip function for further discussion on both operations.