I am trying to boil some data down using Object.keys()
and .map()
. I want to get back an array of objects that contain arrays. The .map()
returns the correct objects (currently two) in the array, but they are duplicated by the number of loops in the .map()
. Below is a simplified example of what I am trying to do
// The data:
const theObjects = {
object1: ['a', 'b', 'c'],
object2: ['d', 'e', 'f'],
object3: ['g', 'h', 'i'],
object4: ['j', 'k', 'l'],
object5: ['m', 'n', 'o'],
};
// Set up the keys for the object I want to get back. It looks weird here, but it is an emulation of data that is extracted from the source of my real dataset:
let season;
const seasons = [
'2021-2022',
'2021-2022',
'2021-2022',
'2022-2023',
'2022-2023',
];
//Set up the return object. Extract the keys from the dataset object and map over them
var compiler = {};
const returnedData = Object.keys(theObjects).map((key, index) => {
// Set up `seasons` to become the object keys of final output and initialize the objects as needed.
var season = seasons[index];
Array.isArray(compiler[season]) ? compiler : (compiler[season] = []);
// `.push()` the current data into the appropriate array:
compiler[season].push(theObjects[key]);
// Test 1: Test the current state of the compiler object and `return` the object:
console.log('COMPILER[', key, ']', index, ': ', compiler);
return compiler;
});
//Test 2: The final object returned
console.log('Returned: ', returnedData);
Test 1 logs once for each loop in the .map()
and we can watch the final object build:
COMPILER[ object1 ] 0 : { '2021-2022': [ [ 'a', 'b', 'c' ] ] }
COMPILER[ object2 ] 1 : { '2021-2022': [ [ 'a', 'b', 'c' ], [ 'd', 'e', 'f' ] ] }
COMPILER[ object3 ] 2 : {
'2021-2022': [ [ 'a', 'b', 'c' ], [ 'd', 'e', 'f' ], [ 'g', 'h', 'i' ] ]
}
COMPILER[ object4 ] 3 : {
'2021-2022': [ [ 'a', 'b', 'c' ], [ 'd', 'e', 'f' ], [ 'g', 'h', 'i' ] ],
'2022-2023': [ [ 'j', 'k', 'l' ] ]
}
COMPILER[ object5 ] 4 : {
'2021-2022': [ [ 'a', 'b', 'c' ], [ 'd', 'e', 'f' ], [ 'g', 'h', 'i' ] ],
'2022-2023': [ [ 'j', 'k', 'l' ], [ 'm', 'n', 'o' ] ]
}
Test 2 logs the final object. The data matches the object returned in the last loop of the .map()
, but it is repeated 5 times:
Returned: [
{
'2021-2022': [
['a', 'b', 'c'],
['d', 'e', 'f'],
['g', 'h', 'i'],
],
'2022-2023': [
['j', 'k', 'l'],
['m', 'n', 'o'],
],
},
{
'2021-2022': [
['a', 'b', 'c'],
['d', 'e', 'f'],
['g', 'h', 'i'],
],
'2022-2023': [
['j', 'k', 'l'],
['m', 'n', 'o'],
],
},
{
'2021-2022': [
['a', 'b', 'c'],
['d', 'e', 'f'],
['g', 'h', 'i'],
],
'2022-2023': [
['j', 'k', 'l'],
['m', 'n', 'o'],
],
},
{
'2021-2022': [
['a', 'b', 'c'],
['d', 'e', 'f'],
['g', 'h', 'i'],
],
'2022-2023': [
['j', 'k', 'l'],
['m', 'n', 'o'],
],
},
{
'2021-2022': [
['a', 'b', 'c'],
['d', 'e', 'f'],
['g', 'h', 'i'],
],
'2022-2023': [
['j', 'k', 'l'],
['m', 'n', 'o'],
],
},
];
I can make this work by simply using returnedData[0]
and calling it a day. Unfortunately, my anal-retentive side just can't let this go. Is there something that I can do to just get back something that looks like the COMPILER[ object5 ]...
output?