Given an array of an even length:
const items = [
'this/is/path1',
'this/is/path2',
'this/is/path3',
'this/is/path4',
'this/is/path5',
'this/is/path6'
];
i want to create an array of objects that will have certain length objnb.
Based on this length, i will divide the above items into chunks, and then store the first index on a new object property path, and the following elements in other1, other2; The same for the other object chunks.
My solution is 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)
the output is correct:
[ { 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' } },
{},
{} ]
but unfortunately, i get empty objects, which i don't want. How can i achieve the same with much cleaner way?
the preferred result will not contain the empty objects.
update
another example is:
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)
and result is:
[ { path: 'this/is/path1', others: { two: 'this/is/path2' } },
{},
{ path: 'this/is/path3', others: { two: 'this/is/path4' } },
{} ]
clarification:
every object in the new array will be alike, for example, they will all have path, and since we are dividing the original array evenly, the new objects will have all the same properties. For example, if one has two, the rest of the objects in the new array will all have this property, and if they are supposed to have three, they all will have that property.
the new object will look like:
{ path: 'this/is/path1',
others: {
two: 'this/is/path2',
three: 'this/is/path3' // optional; if one object has this, others must have it too.
}
}
so basically, by dividing the original items array into a specific number, either 2, 3, 4, etc... we will chunk it into smaller arrays, and the new object path will be chunkedArray[0], and if there is one more item in this new chunked array, then two will be chunkedArray[1], and if still one more left, then three will be chunkedArray[2]
so if we divide it by 2, then we will have:
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
];
therefore the new objects will have two and three;
but if we divide it into 3, we will have:
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
];
so we will have only path and two for each object.
every new chunkedArray will have at least length of two, meaning that path and two is present in every new object.
another example:
and one basic example is, if the original array is three, then we can't divide it evenly to smaller chunks, so :
const items = [
'this/is/path1', //path
'this/is/path2',//others.two
'this/is/path3'//others.three
];
and same here , if the original array is two length:
const items = [
'this/is/path1', //path
'this/is/path2',//others.two
];
others.two, others.three... Or could it beothers._2, others._3..?others[two]andconst two = '2a', for example, andconst three = '3a'