javascript - javascript将数组对象拆分为给定大小的对象数组
问题描述
我有一个具有一些属性的对象,例如:
const rows_obj = {
prova1:[{price:3,description:"test11"},{price:7,description:"test12"}],
prova2:[{price:11,description:"test21"},{price:2,description:"test22"}],
prova3:[{price:1,description:"test31"},{price:23,description:"test32"}],
}
我需要在一页或多页中打印行,所以每页的限制是例如 3 行。所以在这种情况下,我需要一个对象 obj 数组,例如:
obj[0] = {
total:21,
prova1:[{price:3,description:"test11"},{price:7,description:"test12"},],
prova2:[{price:11,description:"test21"}],
}
obj[1] = {
total:26,
prova2:[{price:2,description:"test22"}],
prova3:[{price:1,description:"test31"},{price:23,description:"test32"},],
}
(因为在这种情况下,限制是每页/对象 3 行)
但限制也可能是 20 行,因此最终对象将是:
obj[0] = {
total:47,
prova1:[{price:3,description:"test11"},{price:7,description:"test12"},],
prova2:[{price:11,description:"test21"},{price:2,description:"test22"},],
prova3:[{price:1,description:"test31"},{price:23,description:"test32"},],
}
因为在原始对象中有 6 行,那么,由于在限制之下,函数必须检索一个包含一个元素的数组,并且这个元素等于原始元素。
我试过了,但到目前为止我已经做了这个代码:
const size = 3
const rows_obj = {
prova1:[{price:22,description:"test11"},{price:23,description:"test12"},],
prova2:[{price:22,description:"test21"},{price:23,description:"test22"},],
prova3:[{price:22,description:"test31"},{price:23,description:"test32"},],
}
var rows_length = 0;
for(var char in rows_obj){
// Confirm that the key value is an array before adding the value.
if(Array.isArray(rows_obj[char])){
rows_length += rows_obj[char].length;
}
}
if (!rows_length) {
return [[]]
}
const arrays = []
let i = 0
const keys = Object.keys(rows_obj)
let obj = null
while (i<rows_length) {
obj = {}
for(let j=0;j<keys.length;j++) {
obj[keys[j]] = rows_obj[keys[j]].slice(i, i + size)
i = i + 2 + size
console.log(i)
}
arrays.push(obj)
}
它不工作,我做的一团糟......有什么帮助吗?先感谢您。
解决方案
使用Object#entries
,Array#reduce
和Array#forEach
:
const
rows_obj = {
prova1:[{price:3,description:"test11"},{price:7,description:"test12"}],
prova2:[{price:11,description:"test21"},{price:2,description:"test22"}],
prova3:[{price:1,description:"test31"},{price:23,description:"test32"}],
},
SIZE = 3;
let count = 0; // count of items in last object
const rowEntries = Object.entries(rows_obj);
// iterate over rows_obj entries while updating finalList
const res = rowEntries.reduce((finalList, [currentKey, currentItems]) => {
// iterate over current items
currentItems.forEach(item => {
// if SIZE is reached in the last object, add new one
if(count === SIZE) {
count = 0;
finalList.push({ total: 0 });
}
// update last object
const last = finalList[finalList.length-1];
finalList[finalList.length-1] = {
...last,
total: last.total + item.price,
[currentKey]: [...(last[currentKey] || []), item]
};
count++;
});
return finalList;
}, rowEntries.length > 0 ? [{ total: 0 }] : []);
console.log(res);
推荐阅读
- python - RuntimeError:在请求上下文 Flask 会话之外工作
- javascript - 此版本 ChromeDriver 仅支持 Chrome 版本 85 错误
- apache-kafka - Apache Kafka:一段时间后将消息发送到另一个主题
- angular - Angular 通用 SSR:如何预渲染漂亮的 html
- reactjs - AWS Amplify React 自定义用户输入
- flutter - NTAG213 Mifare Ultralight 带身份验证
- codeigniter - Codeigniter 4 验证总是失败
- python - 如何与 python/pandas 交叉和合并文件以根据原始输入文件将重叠划分为子区域?
- c - 必要时一次性缓冲液
- anaconda - conda:conda update 和 conda upgrade 有什么区别?