javascript将数组对象拆分为给定大小的对象数组

javascript将数组对象拆分为给定大小的对象数组,javascript,algorithm,Javascript,Algorithm,我有一个具有以下属性的对象: const rows_obj = { prova1:[{price:3,description:"test11"},{price:7,description:"test12"}], prova2:[{price:11,description:"test21"},{price:2,description:"test22"}], prova3:[{price:1,de

我有一个具有以下属性的对象:

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[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)
  }
const size=3
常量行\u对象={
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”},],
}
变量行长度=0;
for(行中的var char\u obj){
//在添加值之前,请确认键值是数组。
if(Array.isArray(rows_obj[char])){
行长度+=行对象[char]。长度;
}
}
如果(!行长度){
返回[]]
}
常量数组=[]
设i=0
常量键=对象键(行\u对象)
设obj=null
而(i使用,和:

const
行_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;
let count=0;//最后一个对象中的项目计数
const rowEntries=Object.entries(行\对象);
//在更新finalList时迭代行\u obj条目
const res=rowEntries.reduce((finalList,[currentKey,currentItems])=>{
//迭代当前项目
currentItems.forEach(项目=>{
//如果在最后一个对象中达到大小,请添加新对象
如果(计数==大小){
计数=0;
push({total:0});
}
//更新最后一个对象
const last=finalList[finalList.length-1];
finalList[finalList.length-1]={
…最后,
总计:最后一项总计+项目价格,
[currentKey]:[…(最后一个[currentKey]| |[]),项目]
};
计数++;
});
回归终结者;
},rowcentries.length>0?[{total:0}]:[]);
console.log(res);
使用,以及:

const
行_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;
let count=0;//最后一个对象中的项目计数
const rowEntries=Object.entries(行\对象);
//在更新finalList时迭代行\u obj条目
const res=rowEntries.reduce((finalList,[currentKey,currentItems])=>{
//迭代当前项目
currentItems.forEach(项目=>{
//如果在最后一个对象中达到大小,请添加新对象
如果(计数==大小){
计数=0;
push({total:0});
}
//更新最后一个对象
const last=finalList[finalList.length-1];
finalList[finalList.length-1]={
…最后,
总计:最后一项总计+项目价格,
[currentKey]:[…(最后一个[currentKey]| |[]),项目]
};
计数++;
});
回归终结者;
},rowcentries.length>0?[{total:0}]:[]);

console.log(res);
我觉得这不是最好的解决方案,但效果正如您所期望的

const rows\u 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”},],
}
const rows=Object.entries(rows\u obj).flatMap([k,v])=>(
v、 映射(e=>({key:k,…e}))
))
常数大小=3
常量组=[]
设i=0
while(ie.reduce((acc,{key,…rest})=>{
acc[关键]
?acc[键]。推送({…rest})
:acc[key]=[{…rest}]
返回acc
}, {}))
console.log(res)

.as console wrapper{max height:100%!important;top:0;}
我觉得这不是最好的解决方案,但效果与您预期的一样

const rows\u 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”},],
}
const rows=Object.entries(rows\u obj).flatMap([k,v])=>(
v、 映射(e=>({key:k,…e}))
))
常数大小=3
常量组=[]
设i=0
while(ie.reduce((acc,{key,…rest})=>{
acc[关键]
?acc[键]。推送({…rest})
:acc[key]=[{…rest}]
返回acc
}, {}))
console.log(res)
.as控制台包装{最大高度:100%!重要;顶部:0;}
更新 从问题更新中添加附加要求的总和并不困难:

常量重组=(n)=>(xs)=>
chunk(n)(Object.entries(xs.flatMap([k,xs])=>xs.map(x=>[k,x]))
.map(组=>group.reduce(
({total,…r},[k,v])=>({
总计:总计+v.价格,
R
[k] :(r[k]| |[])concat(v)
}),{总计:0}
))
您可以通过展开此代码段看到这一点:

const chunk=(n)=>(xs)=>
xs.length(xs)=>
chunk(n)(Object.entries(xs.flatMap([k,xs])=>xs.map(x=>[k,x]))
.map(组=>group.reduce(
({total,…r},[k,v])=>({total:total+v.price,
{总数:0}
))
const rows_obj={prova1:[{pr