javascript基于另一个较大的数组创建具有固定长度的对象数组

javascript基于另一个较大的数组创建具有固定长度的对象数组,javascript,arrays,ecmascript-6,Javascript,Arrays,Ecmascript 6,给定偶数长度的数组: const items = [ 'this/is/path1', 'this/is/path2', 'this/is/path3', 'this/is/path4', 'this/is/path5', 'this/is/path6' ]; 我想创建一个具有一定长度的对象数组objnb。 基于此长度,我将把上面的项划分为块,然后将第一个索引存储在新对象属性路径,并将以下元素存储在其他1,其他2;其他对象块也是如此 我的解决方案是hacky: cons

给定偶数长度的数组:

const items = [
  'this/is/path1',
  'this/is/path2',
  'this/is/path3',
  'this/is/path4',
  'this/is/path5',
  'this/is/path6'
];
我想创建一个具有一定长度的对象数组
objnb
。 基于此长度,我将把上面的
划分为块,然后将第一个索引存储在新对象属性
路径
,并将以下元素存储在
其他1
其他2
;其他对象块也是如此

我的解决方案是hacky:

const objnb = 2;
const other1 = true;
const other2 = true;
const outputobjs = items.length / objnb;
const result = items.map((entry, index) => {
  let obj = {};
  console.log({ entry, index })
  if (index % outputobjs === 0) {
    obj.path = entry;
    obj.others = {};
    if (other1) {
      obj.others.two = items[index + 1];
    }
    if (other2) {
      obj.others.three = items[index + 2];
    }

    return obj;
  }

  return obj;
})


console.log('result: ', result)
输出正确:

[ { path: 'this/is/path1',
    others: { two: 'this/is/path2', three: 'this/is/path3' } },
  {},
  {},
  { path: 'this/is/path4',
    others: { two: 'this/is/path5', three: 'this/is/path6' } },
  {},
  {} ]
但不幸的是,我得到了我不想要的空对象。 我怎样才能以更干净的方式达到同样的效果

首选结果将不包含空对象

更新 另一个例子是:

const items = [
  'this/is/path1',
  'this/is/path2',
  'this/is/path3',
  'this/is/path4'
];

const objnb = 2;
const other1 = true;
const other2 = false;
const outputobjs = items.length / objnb;
const result = items.map((entry, index) => {
  let obj = {};
  console.log({ entry, index })
  if (index % outputobjs === 0) {
    obj.path = entry;
    obj.others = {};
    if (other1) {
      obj.others.two = items[index + 1];
    }
    if (other2) {
      obj.others.three = items[index + 2];
    }

    return obj;
  }

  return obj;
})


console.log('result: ', result)
结果是:

[ { path: 'this/is/path1', others: { two: 'this/is/path2' } },
  {},
  { path: 'this/is/path3', others: { two: 'this/is/path4' } },
  {} ]
澄清: 新数组中的每个对象都将是相似的,例如,它们都将具有
路径
,并且由于我们将原始数组平均分割,因此新对象将具有所有相同的属性。例如,如果一个对象有两个,则新数组中的其余对象都将具有此属性,如果它们应该具有三个,则它们都将具有该属性

新对象的外观如下所示:

{ path: 'this/is/path1',
    others: { 
       two: 'this/is/path2', 
       three: 'this/is/path3' // optional; if one object has this, others must have it too.
    }
}
所以基本上,通过将原始项数组划分为一个特定的数字,2,3,4等等。。。我们将它分块成更小的数组,新对象
路径
将是
chunkedArray[0]
,如果这个新的分块数组中还有一个项目,那么
2个
将是
chunkedArray[1]
,如果还有一个,那么
3个
将是
chunkedArray[2]

如果我们把它除以2,那么我们将得到:

const chunkedArray1 = [
  'this/is/path1', // path
  'this/is/path2', //others.two
  'this/is/path3' //others.three
];

const chunkedArray2 = [
  'this/is/path4',// path
  'this/is/path5',//others.two
  'this/is/path6'//others.three
];
const chunkedArray1 = [
  'this/is/path1',// path
  'this/is/path2'//others.two
];

const chunkedArray2 = [
  'this/is/path3',// path
  'this/is/path4'//others.two
];

const chunkedArray3 = [
  'this/is/path5',// path
  'this/is/path6'//others.two
];
因此,新对象将有两个
和三个

但如果我们将其分为3,我们将有:

const chunkedArray1 = [
  'this/is/path1', // path
  'this/is/path2', //others.two
  'this/is/path3' //others.three
];

const chunkedArray2 = [
  'this/is/path4',// path
  'this/is/path5',//others.two
  'this/is/path6'//others.three
];
const chunkedArray1 = [
  'this/is/path1',// path
  'this/is/path2'//others.two
];

const chunkedArray2 = [
  'this/is/path3',// path
  'this/is/path4'//others.two
];

const chunkedArray3 = [
  'this/is/path5',// path
  'this/is/path6'//others.two
];
因此,对于每个对象,我们只有路径和
两个

每个新的chunkedArray至少有两个长度,这意味着路径和
two
存在于每个新对象中

另一个例子: 一个基本的例子是,如果原始数组是3,那么我们不能将它平均分割成更小的块,所以:

const items = [
  'this/is/path1', //path
  'this/is/path2',//others.two
  'this/is/path3'//others.three
];
这里相同,如果原始数组的长度为两个:

const items = [
  'this/is/path1', //path
  'this/is/path2',//others.two
];

简而言之,您可以循环每个第三项并将其作为对象推送

const项=[
“this/is/path1”,
“this/is/path2”,
“this/is/path3”,
“this/is/path4”,
“this/is/path5”,
'此/为/路径6'
];
var结果=[];
对于(变量i=0;iconsole.log(result)
下面是一个使用
reduce
的解决方案

const项=[
“this/is/path1”,
“this/is/path2”,
“this/is/path3”,
“this/is/path4”,
“this/is/path5”,
'此/为/路径6'
]
函数拆分项(数据、块大小){
if(chunkSize<2)返回数据//或您想要的特殊值。
常量标签={
1:"一",,
二:"两",,
三:"三",,
4:‘四’
//……其他
}
返回数据。减少((前、后、索引)=>{
如果(索引%chunkSize==0){
/*老问题
预推({
路径:cur,
其他:{}
})*/
让newItem={
路径:cur,
其他:{}
}
Array.from(数组(chunkSize-1).keys()).map(itemIndex=>{
newItem.others[标签[(itemIndex+1)%chunkSize+1]]=''/或其他默认值
})
预推(新项目)
}
否则{
pre[pre.length-1]。其他[labels[index%chunkSize+1]]=项[index]
}
返回前
}, [])
}
log('@testcase1@',splitItems(items,2),'@'))
console.log('@testcase2@',splitItems(items.slice(0,2,2),'@'))
console.log('@testcase3@',splitItems(items.slice(0,4,2),'@'))
console.log('@testcase4@',splitItems(items.slice(0,5,3),'@'))
//首先计算大小,然后执行拆分项
函数splitByLength(数据,numberOfChunks){
让chunkSize=Math.round(data.length/3,0)
返回拆分项(数据、块大小)
}

console.log('@testcase 5@',splitByLength(items.slice(0,5,3),'@')
另一个使用reduce的解决方案,与@sphinx answer略有不同,尽管非常相似:

const objnb = 3;
const otherKeys = ['two', 'three'];

const items = [
  'this/is/path1',
  'this/is/path2',
  'this/is/path3',
  'this/is/path4',
  'this/is/path5',
  'this/is/path6'
];

const outputobjs = Math.ceil(items.length / objnb);

const ar = items.reduce((memo, item, index) => {
  const mod = index % outputobjs
  if(mod === 0)
    memo.push({path: item, others: {}});
  else if(mod <= otherKeys.length)
    Object.assign(memo[memo.length - 1].others, {[otherKeys[mod -1]]: item});
  return memo;
}, []);

console.log(ar);
const objnb=3;
const otherkey=['two','three'];
常数项=[
“this/is/path1”,
“this/is/path2”,
“this/is/path3”,
“this/is/path4”,
“this/is/path5”,
'此/为/路径6'
];
const outputobjs=Math.ceil(items.length/objnb);
常量=项目。减少((备忘录、项目、索引)=>{
const mod=索引%outputobjs
如果(mod==0)
push({path:item,others:{}});

否则如果(mod这只适用于给定的示例。请参阅我在帖子中包含的另一个示例。我认为有点误解,我在问题中澄清了这一点。它必须是
其他。2,其他。3
…还是
其他。_2,其他。_3..
?@Logar实际上我有一个特定的密钥名,这将是一些像<代码>其他[两个]
常数二='2a'
常数三='3a'
好的,我的问题基本上是,我们可以使用“类似索引”来生成键,还是必须手动设置键?我有特定的键名,我必须使用它们。我想这有点误解,我在question@storis我不确定你说得对,你对我的回答唯一的担心是你想把项目分成不同的块,而不是分成不同长度的块?如果是这样的话,我只是重复使用了你的
outputobjs
,它工作了。我想有点误解,我在问题中澄清了它工作正常,但是我更喜欢您在参数中传递的chunkSize来表示新数组中的对象数。如果原始项的长度为2或3,我添加了另一个说明。很简单,只需先计算chunkSize,然后执行
f即可