I’m trying to get the first element of each object ip and then concat them and push them in an array, then take the second element and so on..
here is the grouped by ip array
{
"ip1.x.x.x": [
[
{
"coin": "coin1"
},
{
"coin": "coin2"
},
{
"coin": "coin3"
},
{
"coin": "coin4"
},
{
"coin": "coin5"
}
],
[
{
"coin": "coin6"
},
{
"coin": "coin7"
},
{
"coin": "coin8"
},
{
"coin": "coin9"
}
]
],
"ip2.x.x.x": [
[
{
"coin": "coin10"
},
{
"coin": "coin11"
},
{
"coin": "coin12"
},
{
"coin": "coin13"
},
{
"coin": "coin14"
}
],
[
{
"coin": "coin15"
},
{
"coin": "coin16"
}
]
],
"ip3.x.x.x": [
[
{
"coin": "coin17"
},
{
"coin": "coin18"
},
{
"coin": "coin19"
},
{
"coin": "coin20"
},
{
"coin": "coin21"
}
],
[
{
"coin": "coin22"
},
{
"coin": "coin23"
},
{
"coin": "coin24"
}
]
],
etc..
}
the expected result should be as follows
[
[
{
"coin": "coin1"
},
{
"coin": "coin2"
},
{
"coin": "coin3"
},
{
"coin": "coin4"
},
{
"coin": "coin5"
},
{
"coin": "coin10"
},
{
"coin": "coin11"
},
{
"coin": "coin12"
},
{
"coin": "coin13"
},
{
"coin": "coin14"
},
{
"coin": "coin17"
},
{
"coin": "coin18"
},
{
"coin": "coin19"
},
{
"coin": "coin20"
},
{
"coin": "coin21"
}
],
[
{
"coin": "coin6"
},
{
"coin": "coin7"
},
{
"coin": "coin8"
},
{
"coin": "coin9"
},
{
"coin": "coin15"
},
{
"coin": "coin16"
},
{
"coin": "coin22"
},
{
"coin": "coin23"
},
{
"coin": "coin24"
}
]
]
as you can see I took the 1st element in the array of each ip and concat and pushed them to new array, then I took the 2nd element, and if there is 3rd element I’ll keep doing so till the object keys are done.
Now what I’ve tried to do is the following
const chunkArray = (array, chunkSize) => {
return Array.from({
length: Math.ceil(array.length / chunkSize)
}, (_, index) =>
array.slice(index * chunkSize, (index + 1) * chunkSize)
);
};
let data_in_chunks_array = [];
let data_obj = {};
for (let i = 0; i < Object.keys(groupedByIp).length; i++) {
const ip = Object.keys(groupedByIp)[i];
let data_in_chunks_ip = chunkArray(groupedByIp[ip], 5);
data_obj[ip] = data_in_chunks_ip;
}
console.log('data_obj', data_obj);
<script>
let groupedByIp = {
'ip1.x.x.x': [{
coin: 'coin1',
},
{
coin: 'coin2',
},
{
coin: 'coin3',
},
{
coin: 'coin4',
},
{
coin: 'coin5',
},
{
coin: 'coin6',
},
{
coin: 'coin7',
},
{
coin: 'coin8',
},
{
coin: 'coin9',
},
],
'ip2.x.x.x': [{
coin: 'coin10',
},
{
coin: 'coin11',
},
{
coin: 'coin12',
},
{
coin: 'coin13',
},
{
coin: 'coin14',
},
{
coin: 'coin15',
},
{
coin: 'coin16',
},
],
'ip3.x.x.x': [{
coin: 'coin17',
},
{
coin: 'coin18',
},
{
coin: 'coin19',
},
{
coin: 'coin20',
},
{
coin: 'coin21',
},
{
coin: 'coin22',
},
{
coin: 'coin23',
},
{
coin: 'coin24',
},
],
};
</script>
This will return the first example data. Now I can’t figure out the second part where I take the 1st element of each ip array and concat them and keep doing so till they are all have been taken, like (remove at index) for example.
2
Answers
I com up with a solution as follows
It seems to work for me, it's based on lodash lib
This conversion has the concept of [transposing a maxtrix), together with concatenating the inner arrays (for which
flatMap
can serve), and ignoring the keys of the original object (for which we can useObject.values
).So: